Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C-problem (and it makes me mad)

Status
Not open for further replies.

fheyn

Programmer
Joined
Mar 22, 2001
Messages
198
Location
DE
Hi,
there are days...
OK, my problem is like this :
I'm passing an array of char (cArray[50][20]) to a function which is declared like this : MyFunc(char *pArray). with pArray I'd like to access the array-items, which it does if I increment it by 20, but then it only gives me the first character of each array-item. To correct this, I use
strcpy(..) and all goes fine, but to me it looks like a really bad solution.

Someone has an idea to improve this ?
thanks
 
I am not sure the following answer is the right one:

What you want to pass to the function is a 2-dimension array(Actually a pointer which points to the beginning address of the array), and the function declaration shows that you pass a char pointer. It's okay, on problem. The key is how to access the items in your function, you can do like the following:

MyFunc(char* pArray)
{
typedef char [50][20] myArrayType;
myArrayType * pWork = (myArrayType*)pArray;
//Now you can use pWork
pWork->[0][1] = 20; //Something like this.
...
}

Hope this can help.
 
Sorry the correct code is the following:

void MyFunc(char* pArray)
{
typedef char myArrayType[50[20];
myArrayType * pWork = (myArrayType *)pArray;
//Now you cna use pWork
(*pWork)[0][0] = 20; //Something like this.
....
}

Sorry for the mistake.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top