ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm pretty experienced in C - I have worked with C for 9 to 10 years and have done some kernel programming. I was dumbfounded when I realized I couldn't fix this problem. I had always been thinking char **x = char x[10][20] are same except for the fact that no memory (excluding the 4 bytes for the pointer) is reserved for the former case. Looks like its not! I feel like a fool because all these years I thought I was good with C pointers. Help me!
This is what I want to do (kinda pseudo-code - of course the actual code is much more
complex, but this should be enough to get the point across) -
Code:
main()
{
char list[2][10] = {"Hello","World"};
somefunc( pass list some how );
printf("%s -- %s\n", list[0], list[1]);
}
somefunc(param of some data type called "thatlist")
{
strcpy(thatlist[0], "Test1");
strcpy(thatlist[1], "Test2");
}
What was wrong with my previous code? if I can access char **thatlist as thatlist[1][3], etc why is char list[2][10] not same as thatlist? Also what datatype is thatlist in the declaration -
Code:
void somefunc(char thatlist[2][10])
All these days I was thinking the above declaration is not valid or it is a pass by copy. Guess I was wrong. Please explain why my code was wrong and why your code is right. I tested your code and it works, but y? Also, what if I don't know how big x and y are in the declaration char list[x][y]? How should I write a function to handle such a case?
Thanks a lot,
Sarav
Last edited by saravkrish; 12-01-2004 at 11:23 PM.
I had tried that before asking this question. To rephrase my first question, in actual use, I don't know how big list[][] will be. I pass another param that tells somefunc() how many rows of list there are, so that it doesn't go out of bounds.
How would I code that requirement?
Hari,
Can you explain in words how char **x and char x[2][10] are different? For example, char **x is a pointer to a pointer to a char whereas .... blah blah.
The compiler needs to know at compile time the number of columns in a row on a 2dim array to calculate the row offset when you try to use var[][] syntax. Internally it has to use an assembly multiply instruction, but it must know what to multiply your row variable by.
If you don't know the array dimensions at compile time then you can't use [][] to access elements in the array. You'll have to pass a pointer to the first element of the array to the function along with the 2dim array width/height as separate integer arguments, then do the multiply and adding in the function yourself.
I don't know of any other way.
Is there any other way other than randy's suggestion to pass char *, row and col? It would be nice if I could use thatlist[0], thatlist[1], etc.
Randy,
Thanks. Think it makes sense.
Hari,
I repeat - the actual code is much more complex. Please don't change the question. I want to use strcpy for some reason. I can't paste 300+ lines of code to tell you why I need a strcpy!!!
In that case, I cannot understand your question. You seem to be assuming that everybody is born to read your mind so how on earth am I supposed to understand your question in the first place? I can only solve your problem with the PROGRAM THAT YOU HAVE PRESENTED HERE and you don't even care to thank me!
Why don't you create a String class yourself and implement that bit of functionality instead of using raw pointers? Pointers are more confusing to use when you start having pointer-to-pointer.
You can store the rows and cols as member variables in the class.
One of the differences between a char** and char[][] is that with a char[][] you will have a contiguous memory block, and a char** will be a pointer to an array of pointers, which themselves are pointers to a char....
If you consider for a moment what you have to do to dynamically allocate memory for a char**, it might make more sense.
Code:
char** pBlah = new char*[10];
for (int c=0;c<10;c++)
{
pBlah[c] = new char[10];
}
Obviously, you are not going to have a contiguous block of memory in the above case...
One possible alternative to your problem might be do pass your data in as if it were a single dimension array. For instance something like so might work:
Code:
#include <stdio.h>
void DumpMatrix(int *pVals, int rows, int columns);
int main()
{
int matrix[4][4];
int x, y;
for (y=0;y<4;y++)
{
for (x=0;x<4;x++)
{
matrix[y][x] = y*4+x;
}
}
DumpMatrix((int*)matrix, 4, 4);
return 0;
}
void DumpMatrix(int *pVals, int rows, int columns)
{
int x,y;
for (y=0;y<rows;y++)
{
for (x=0;x<columns;x++)
{
int index = y*columns+x;
printf("%d ", pVals[index]);
}
printf("\n");
}
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.