?
Solved

Easy beginner C question

Posted on 2003-03-06
19
Medium Priority
?
167 Views
Last Modified: 2010-04-15
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.
0
Comment
Question by:Leafox
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 4
  • 2
  • +2
19 Comments
 
LVL 6

Expert Comment

by:gj62
ID: 8082000
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.

0
 
LVL 6

Expert Comment

by:gj62
ID: 8082043
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...
0
 

Author Comment

by:Leafox
ID: 8082130
hey gi62
But can i allocate memory to *pointer using malloc and be able to increment and decrement it ?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Leafox
ID: 8082132
*point
0
 
LVL 6

Expert Comment

by:gj62
ID: 8082176
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
0
 
LVL 6

Expert Comment

by:gj62
ID: 8082197
"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...
0
 
LVL 6

Expert Comment

by:gj62
ID: 8082248
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...
0
 

Author Comment

by:Leafox
ID: 8082295
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.

   
0
 
LVL 6

Expert Comment

by:gj62
ID: 8082363
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...

0
 
LVL 6

Accepted Solution

by:
gj62 earned 200 total points
ID: 8082388
If you really wanted to pass the entire array, and work with it in the function, you could do the following:

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


main()
{
  char words[5000][10];
  char (*ptr)[10];

  ptr=words;  
  mean2(ptr);
}

If you want to do this, I'd really consider using an array of pointers...
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8085208
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];

0
 
LVL 6

Expert Comment

by:gj62
ID: 8085535
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... "
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8085569
ohh i missed that one .. need to check up my eyes
0
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 8086061
use this

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

Now you can access the array using ptr.
0
 
LVL 6

Expert Comment

by:gj62
ID: 8088585
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...
0
 
LVL 5

Expert Comment

by:cryptosid
ID: 8093435
>>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
0
 
LVL 5

Expert Comment

by:cryptosid
ID: 8093443

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
0
 
LVL 6

Expert Comment

by:gj62
ID: 8094060
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...
0
 

Author Comment

by:Leafox
ID: 8094350
Thanks a lot!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question