The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Jagged arrays using pointers and malloc
Discuss Jagged arrays using pointers and malloc in the C Programming forum on Dev Shed. Jagged arrays using pointers and malloc C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

December 11th, 2012, 12:56 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 3
Time spent in forums: 46 m 56 sec
Reputation Power: 0
|
|
|
Jagged arrays using pointers and malloc
Hello all. I'm a beginner in C and I'm having trouble making a jagged array and understanding how to use it using pointers and malloc.
I would like to be able to make an array of strings. (I'm making a parseline function.)
How do I convert
char words[10][50];
words[0][0]='h';
words[0][1]='i';
words[1][0]='w';
words[1][1]='o';
words[1][2]='r';
words[1][3]='l';
words[1][4]='d';
printf("%s \n",words[0]); //Output is hi
printf("%s \n",words[1]); //Output is world. It's supposed to be but it's printing worldi for some reason.
in terms of pointers and malloc?
|

December 11th, 2012, 01:03 PM
|
 |
Contributed User
|
|
|
|
> //Output is world. It's supposed to be but it's printing worldi for some reason.
Where are these lines?
Code:
words[0][0]='h';
words[0][1]='i';
words[0][2]='\0';
words[1][0]='w';
words[1][1]='o';
words[1][2]='r';
words[1][3]='l';
words[1][4]='d';
words[1][5]='\0';
If you don't end your strings properly with a \0, you're likely to print garbage.
What kind of dynamic array do you want?
Code:
char words[10][50]; // fixed length words, fixed number of words
char *words[10]; // variable length words, fixed number of words
char (*words)[50]; // fixed length words, variable number of words
char **words; // variable length words, variable number of words
|

December 11th, 2012, 01:09 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 3
Time spent in forums: 46 m 56 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by salem > //Output is world. It's supposed to be but it's printing worldi for some reason.
Where are these lines?
Code:
words[0][0]='h';
words[0][1]='i';
words[0][2]='\0';
words[1][0]='w';
words[1][1]='o';
words[1][2]='r';
words[1][3]='l';
words[1][4]='d';
words[1][5]='\0';
If you don't end your strings properly with a \0, you're likely to print garbage.
What kind of dynamic array do you want?
Code:
char words[10][50]; // fixed length words, fixed number of words
char *words[10]; // variable length words, fixed number of words
char (*words)[50]; // fixed length words, variable number of words
char **words; // variable length words, variable number of words
|
I see. I really want variable length words and number of words because it's based on whatever the user gives.
Am I supposed to use *(*(words+j)+k) when I'm adding a letter to a word and *(words+j) when I'm trying to print the word? Or since it's a pointer to a pointer of chars I need to iterate to print the whole word?
|

December 11th, 2012, 01:21 PM
|
 |
Contributed User
|
|
|
|
|
Well you would start things off with
char **words = malloc( numWords * sizeof(char*) );
Then later
words[0] = malloc( (strlen(aWord)+1) * sizeof(char) );
strcpy(words[0], aWord);
If you don't know numWords in advance, then just make a guess and read up on realloc() so you can extend the array of pointers as needed.
|

December 11th, 2012, 01:47 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 3
Time spent in forums: 46 m 56 sec
Reputation Power: 0
|
|
Thank you so much! Your explanation of how to allocate memory for pointers to pointers and the importance of inserting the '\0' was extremely helpful! Everything is working fine now. 
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|