Link to home
Start Free TrialLog in
Avatar of Leafox
Leafox

asked on

Easy beginner C question

I have global array of 5000 strings of length 10 characters as words[5000][10]. I want to store these strings in a pointer *point. How would i do this

point = words didnt work for me. Please help.
Avatar of gj62
gj62

It would help to know why you are trying to do this.

Of course, you can just do:

char words[5000][10];
char * point;

point = (char *) words;

That will create a pointer to words, but it would NOT be able to increment and decrement...

Why are you trying to do this? When you pass words to a function, it will decay into a pointer anyhow.

Just to clarify, when you say

"I want to store these strings in a pointer * point", that really isn't how it works.

A pointer is just that - it "points" to where stuff is stored.  A pointer ONLY stores an address.

Is what you want really an array of pointers to strings?  That's a bit different...
Avatar of Leafox

ASKER

hey gi62
But can i allocate memory to *pointer using malloc and be able to increment and decrement it ?
Avatar of Leafox

ASKER

*point
Anyhow, if you REALLY wanted to get that pointer, you could do:

char words[5000][10];
char (*ptr)[10];
ptr=words;

But that would not allow you to subscript ptr to get at the array elements...

For a good explanation of why you don't want to do what you ask, see the C FAQ:

http://www.eskimo.com/~scs/C-faq/q6.13.html
"hey gi62
But can i allocate memory to *pointer using malloc and be able to increment and decrement it ? "

Yes, you can, e.g.

int nrows = 5000;
int ncols = 10;
char **point = (char **)malloc(nrows * sizeof(char *));

for(i = 0; i < nrows; i++)
     point[i] = (char *)malloc(ncolumns * sizeof(char));

(In real code, of course, all of malloc's return values would be checked.)

point[i] now is a char *, which you can manipulate, pass to a function, etc...
Realize that point is now an array of pointers, and as such, does not contain the actual strings themselves, just pointers to them...

Graphically, you have an array of 5000 pointers.  On a 32 bit machine, each pointer is going to be 4-bytes, so your array will take 20,000 bytes of memory.  

When you go through the for loop, each time you will store the pointer (the location in memory), where 10 bytes of string space is stored for your use.  Remember this memory is not guaranteed to be contiguous, but that really shouldn't matter much to you.

This approach does require the extra space for the pointers (an extra 20,000 bytes), but is much more flexible, and makes it much easier if you have to sort, or expand the size of the array.  

Lastly, remember that you have room for 9 characters in each of your strings, plus the terminating null character...
Avatar of Leafox

ASKER

OK..this is kinda where iam actually stuck in the program:

i have global variable :
words[5000][10]  //storing 5000 strings of length 10

then i have function in my program :

void mean(char *data)
 {
   printf("%s",data[0]);
 }

Now when i want to call this function in my
main()
{
..
mean(words); //that doesnt work

}

so whats the correct way of doing this, please help.

   
That shouldn't even compile...

You can't pass in the entire array when your function is expecting just a char *.

Instead, do the following:

void mean(char *data)
{
  printf("%s",data);
}

main()
{
  mean(words[0]); // pass just 1 string...
}

That would print your first string...

ASKER CERTIFIED SOLUTION
Avatar of gj62
gj62

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
to store strings of length 10 you will need character array of length 11
So above mentioned data structures shud be modified to this
char words[5000][11];
char (*ptr)[11];

Thanks, akshayxx

That was covered 4 posts ago <grin>...

"Lastly, remember that you have room for 9 characters in each of your strings, plus the terminating null character... "
ohh i missed that one .. need to check up my eyes
use this

char **ptr;
ptr = (char **) words;

Now you can access the array using ptr.
Rajeev -

NO, you can't, not if words is defined as char words[5000][10].

A char** is a pointer to a pointer.  If you try to subscript it, it will treat that location as a pointer - not as a string.

When you define something as char[5000][10] there are no pointers.  The initial array reference does DECAY into a pointer - but a pointer to char[][10], not a pointer to a pointer.

Please read the entire thread before responding.  This was covered earlier, and references to the C FAQ given that describe why you can't do what you claim...
>>Thanks, akshayxx

>>That was covered 4 posts ago <grin>...

>>Please read the entire thread before responding.  This was covered earlier, and references to the C FAQ given that describe why you can't do what you claim...


Relax and COOL DOWN gj62 dont make an issue out of small things its for Leafox to decide what he wants...this is a public place and everyone has a right to express whatever he/she feels...cause we are all here to increase our knowledge and share it with others...if u have some thing to correct u can do it in a polite manner!!!

keep it cool..keep it simple
cryptosid

Anyways Leafox i think this thing will work...

if your string is of 10 characters make your array of 11 characters ( the second dimension i mean)

it should be words[5000][11];

/* change this  line data[10] to data[][11] ...*/
void mean(char data[][11])
{
  printf("%s",data[0]);
}

Now when i want to call this function in my
main()
{
..
mean(words); //that doesnt work

}

While passing multi-dimension arrays to functions u can specify one dimension less than the actual dimensions...i.e in case of 2D array u can specify as described above...in case 3D array u specify 2 of dimensions...ex. data[][20][11]

and so on...like for N dimensional array u specify N-1 dimensions..

got it...

hope that helps
cryptosid

and yes gj62 correct me if I am wrong it would be my pleasure to know...!!!!!

cheers
cryptosid,

notice the <grin> on my post to askshayxx - grin, as in smile - no harm, no foul...

however, when someone gives an answer that is just plain wrong and won't work, especially when it was covered earlier in the post as being wrong, I don't think it is unreasonable to ask that person to be a bit more careful.

I realize this is an open forum, but if you aren't sure of an answer, at least say, *I think*, or something to that regard.  I don' think that's too much to ask...
Avatar of Leafox

ASKER

Thanks a lot!