C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 11th, 2012, 12:56 PM
am3nd0za am3nd0za is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 3 am3nd0za User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Reply With Quote
  #2  
Old December 11th, 2012, 01:03 PM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,840 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 19 h 21 m 53 sec
Reputation Power: 1774
> //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
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

Reply With Quote
  #3  
Old December 11th, 2012, 01:09 PM
am3nd0za am3nd0za is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 3 am3nd0za User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Reply With Quote
  #4  
Old December 11th, 2012, 01:21 PM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,840 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 19 h 21 m 53 sec
Reputation Power: 1774
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.
Comments on this post
am3nd0za agrees!

Reply With Quote
  #5  
Old December 11th, 2012, 01:47 PM
am3nd0za am3nd0za is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 3 am3nd0za User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Jagged arrays using pointers and malloc

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap