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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Thanks Lim, need a little more info PLEASE

Status
Not open for further replies.

gander

Programmer
Aug 11, 2003
5
US
Thanks fo your help Lim
I picked up some good tips
I was as you suspected trying to create an array
holding 20 names *name[21]

I am just learning about pointers and was 'trying' to use pointers to manipulate the input and output

also I tried to crate an array names[21][30]
and set a pointer for ir ie *n_ptr = names but I got an error

I am not familiar with malloc
is there a way of doing what I was trying to do just using pointers?

Thanks again
 
Ok:
char name[21][30] = {"Jim",.....,"Gander"};//21 all together
char (*n_ptr)[30] = name; // this is a pointer to array of char[30]
This will work. And you can do n_ptr+1 to acces next name.
The different:
name [21][30] is an array. It is already allocated and size of this array can not be changed and you can not free it. This array can store 21 names and each name can not be longer then 29 (don't forget that each string is terminated by '/0').
(*n_ptr)[30] is just a pointer. There is no room to store any values you have to allocate space by malloc like this:
n_ptr = malloc (21 * sizeof (char[30]); to allocate room for 21 names 29 lengh each and don't forget to free this memory when you don't need this.
Or you can put ptr to already allocated array (seefirst lines);

You better start with reading some book this will save you a lot of time. Don't be afraid to spend more to understand what is the pointer in C without it you will not be able to program.

Try to find the difference between:
char *name[30];
and
char (*name)[30];

Good luck.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top