how function return array

376adb899db5f03e37f75e7b88d4aeaa
0
akmal 101 Sep 22, 2006 at 06:57

Help please.
I have class “smartcard” in vc.net 2003 in which I have char array variable
“DevNames”
and also have function. I want that GetDeviceNames function return array like

char DevNames[16][16]; //class level variable

char GetDeviceNames()
{
return DevNames;
}

then generates compile time error
return : cannot convert from ‘char[16][16] to char

how will the function return array?

7 Replies

Please log in or register to post a reply.

87e614b8b888bb2c4485c1ac16d8c779
0
moe 101 Sep 22, 2006 at 07:55

An array is a collection of pointers meaning char[0] is a char* (pointer) to the first element of your array. You can then for example walk through the array using pointer aritmetics. For you this means you should be able to return a char* just make sure your pointer actually points to the first element of your array.

char* GetDeviceNames()
{
    return DevNames[0];
}
0684f9d33f52fa189aad7ac9e8c87510
0
baldurk 101 Sep 22, 2006 at 08:44

note that this requires you to know the dimensions of the array so that you don’t overstep its bounds. For dynamically allocated arrays this normally means passing out a size as well. If you know yourself what the size is, you don’t have to pass it out, but it’s a good idea to use a const int instead of a “magic number”, since then any changes affect all the code using it.

340bf64ac6abda6e40f7e860279823cb
0
_oisyn 101 Sep 22, 2006 at 10:29

@moe

An array is a collection of pointers meaning char[0] is a char* (pointer) to the first element of your array.

No, an array of pointers is an array of pointers. A two-dimensional array doesn’t contain pointers - every top-level element contains an array. Of course, every array can be converted to a pointer, which in this case is a char(*)[16];

The function then becomes:

char (*GetDeviceNames())[16]
{
    return DeviceNames;
}

Of course, a typedef would be more readable

87e614b8b888bb2c4485c1ac16d8c779
0
moe 101 Sep 22, 2006 at 11:13

.oisyn:

what i meant is this:

char foo[10];
is similar to
char* foo = new char[10];

so you have an array of chars but you can access the first element using a pointer. What he has to do is return a pointer to the first element instead of returning the entire array.

example:

int foo[5] = { 1, 2, 3, 4, 5};  // direct array initialisation
int *p_foo = foo;               // pointer to the first element from the array
cout << p_foo[2] << endl;       // array-syntax for pointer 
cout << *(foo+2) << endl;       // pointer-syntax for arrays
p_foo += 2;                     // pointer aritmetics
cout << *p_foo << endl;         // output is 3
foo += 2;                     // illegal, no pointer arithmetics for arrays!

As a hobby coder i might not have choosen wise words for my previous explanation :unsure:
I am not always sure how things are called in the professional world of coding. Sorry for any confusion.

376adb899db5f03e37f75e7b88d4aeaa
0
akmal 101 Sep 22, 2006 at 13:31

Hi
Every body
Please help me and solve my problem
I have 2 array DevNames returns first function “GetDeviceName”
And I call this function “GetDeviceName” in another function “PopulateNewarray” to populate new array “NewArray”
My code is:
char DevNames[16][16]; //class level variable
char NewArray[16][16]; //class level variable

char (*GetDeviceNames())[16]
{
return DevNames;
}

int PopulateNewArray()
{
NewArray = GetDeviceNames;
Return 1;
}
and error is:
error C2440: ‘=’ : cannot cnvert from ‘char(*)[16]’ to ‘char[16][16]’

A8433b04cb41dd57113740b779f61acb
0
Reedbeta 167 Sep 22, 2006 at 20:19

First, akmal, I merged your new thread back into this one, since they’re still on the same topic.

Second, you can’t assign arrays with ‘=’ in C. What you need to do is loop through each element of DevNames and copy each one into NewArray. Of course, since the elements are themselves arrays, you will need two nested loops (or you could just use strcpy for the inner one).

However, this is all kind of ugly and error-prone. If you want an array of strings I would advise you to use std::vector<std::string>. It will save you the trouble of memory management and you will be able to copy the whole structure with ‘=’ like you want to.

0684f9d33f52fa189aad7ac9e8c87510
0
baldurk 101 Sep 23, 2006 at 10:54

(or you could use strcpy for the inner one).

Provided of course these are null terminated strings :).