C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 03-19-2009, 12:49 AM   #1
Registered User
 
Join Date: Feb 2009
Posts: 19
dynamically allocated strings??

Ok, part of my assignment for school is to create a struct with 2 char pointers in it. These pointers are supposed to point to a couple of dynamically allocated strings and I'm supposed to create a little function that prints the contents of the struct out. So far I have the struct:
Code:
typedef struct
{
	char *name;
	char *number;
}myStruct;
And I have the print function:
Code:
void printMyStruct(myStruct *a, myStruct *b)
{
     printf("%s: %s\n", *(a).name, *(b).number);
}
I think those two parts are right. My question is about the dynamically allocated strings that the pointers in my struct are supposed to point to. I'm not quite sure of how to do that..
CS_Student8337 is offline   Reply With Quote
Old 03-19-2009, 12:56 AM   #2
Resu Deretsiger
 
Nightowl's Avatar
 
Join Date: Nov 2008
Location: /dev/null
Posts: 185
Use the malloc() function.

For example:

Code:
char *string = malloc(sizeof(char) * 100);
. . . for a string of a size of 100 characters.

EDIT: don't forget to free the memory when you're done with it . . .
__________________
Do as I say, not as I do . . .

Experimentation is the essence of programming. Just remember to make a backup first.

"I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

Questions posted by these guidelines are more likely to be answered.

Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.
Nightowl is offline   Reply With Quote
Old 03-19-2009, 12:58 AM   #3
CSharpener
 
vart's Avatar
 
Join Date: Oct 2006
Posts: 5,314
second part is wrong . has precedence over the * so it should not even compile

should be

printf("%s: %s\n", a->name, b->number);

to allocate string use malloc

Code:
a->name = malloc(strlen(temp)+1);
if(a->name)
    strcpy(a->name,temp);
__________________
If I have eight hours for cutting wood, I spend six sharpening my axe.
vart is offline   Reply With Quote
Old 03-19-2009, 01:12 AM   #4
Registered User
 
Join Date: Feb 2009
Posts: 19
I thought that's what it was but I wasn't sure because the instructions say that the strings shouldn't have any wasted space.. So, is it ok to do something like this?

Code:
int capacity = 1;
int size = 0;
int c;
char *string = malloc(sizeof(char) * capacity);
while((c = fegetc(stdin) != EOF)
{
     string[size++] = c;
     if(size == capacity)
     {
          string = realloc ( string ,capacity * sizeof (char));
     }
CS_Student8337 is offline   Reply With Quote
Old 03-19-2009, 01:15 AM   #5
Resu Deretsiger
 
Nightowl's Avatar
 
Join Date: Nov 2008
Location: /dev/null
Posts: 185
Yes, it is, but be careful, because realloc() and malloc() can fail . . .
__________________
Do as I say, not as I do . . .

Experimentation is the essence of programming. Just remember to make a backup first.

"I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

Questions posted by these guidelines are more likely to be answered.

Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.
Nightowl is offline   Reply With Quote
Old 03-19-2009, 01:17 AM   #6
Registered User
 
Join Date: Feb 2009
Posts: 19
Right. I didn't include any code to check if they return NULL. I know I should for my program though. In the code I just posted I used fgetc(). Would it be possible to substitute fgets() instead?
CS_Student8337 is offline   Reply With Quote
Old 03-19-2009, 01:19 AM   #7
Resu Deretsiger
 
Nightowl's Avatar
 
Join Date: Nov 2008
Location: /dev/null
Posts: 185
Hmm. Yes, it is possible, but it would be quite useless, as you'd be using it in the same position as fgetc() unless you read into a huge temporary buffer.
__________________
Do as I say, not as I do . . .

Experimentation is the essence of programming. Just remember to make a backup first.

"I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

Questions posted by these guidelines are more likely to be answered.

Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.
Nightowl is offline   Reply With Quote
Old 03-19-2009, 01:20 AM   #8
Registered User
 
Join Date: Feb 2009
Posts: 19
Cool. Thank you. I'll post again if I run into any problems
CS_Student8337 is offline   Reply With Quote
Old 03-19-2009, 01:26 AM   #9
Registered User
 
Join Date: Dec 2006
Location: Canada
Posts: 2,003
Quote:
Code:
char *string = malloc(sizeof(char) * 100);
. . . for a string of a size of 100 characters.
You meant 99?
cyberfish is offline   Reply With Quote
Old 03-19-2009, 01:39 AM   #10
Resu Deretsiger
 
Nightowl's Avatar
 
Join Date: Nov 2008
Location: /dev/null
Posts: 185
Yes, yes, I'm sorry. My brain's on holidays tonight.

A string of 100 including the NULL, is what I meant . . .
__________________
Do as I say, not as I do . . .

Experimentation is the essence of programming. Just remember to make a backup first.

"I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

Questions posted by these guidelines are more likely to be answered.

Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.
Nightowl is offline   Reply With Quote
Old 03-19-2009, 01:45 AM   #11
Registered User
 
Join Date: Feb 2009
Posts: 19
Ok. One quick, little question. How do I get one of the pointers in my struct to point to the string??
CS_Student8337 is offline   Reply With Quote
Old 03-19-2009, 01:47 AM   #12
Resu Deretsiger
 
Nightowl's Avatar
 
Join Date: Nov 2008
Location: /dev/null
Posts: 185
Like so . . .
Code:
a->name = localstring;
. . . for example. localstring is the malloc'd char *.
__________________
Do as I say, not as I do . . .

Experimentation is the essence of programming. Just remember to make a backup first.

"I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

Questions posted by these guidelines are more likely to be answered.

Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.
Nightowl is offline   Reply With Quote
Old 03-19-2009, 01:55 AM   #13
Registered User
 
Join Date: Feb 2009
Posts: 19
Hmm.. that's how I was trying to do it but I keep getting this error when I try to compile:

error: invalid type argument of ‘->’
CS_Student8337 is offline   Reply With Quote
Old 03-19-2009, 02:05 AM   #14
CSharpener
 
vart's Avatar
 
Join Date: Oct 2006
Posts: 5,314
Quote:
Originally Posted by CS_Student8337 View Post
I thought that's what it was but I wasn't sure because the instructions say that the strings shouldn't have any wasted space..
in this case - use temporary static buffer to enter string and after that use strlen to detrmine the needed space to be allocated like in my example
__________________
If I have eight hours for cutting wood, I spend six sharpening my axe.
vart is offline   Reply With Quote
Old 03-19-2009, 02:06 AM   #15
CSharpener
 
vart's Avatar
 
Join Date: Oct 2006
Posts: 5,314
Quote:
Originally Posted by CS_Student8337 View Post
Hmm.. that's how I was trying to do it but I keep getting this error when I try to compile:

error: invalid type argument of ‘->’
is a pointer or struct?
__________________
If I have eight hours for cutting wood, I spend six sharpening my axe.
vart is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamically allocated size maverickbu C++ Programming 12 06-26-2007 01:16 PM
Build an array of strings dynamically Nazgulled C Programming 29 04-07-2007 09:35 PM
scope of dynamically allocated variables lanzyzhang C Programming 4 07-20-2004 10:59 AM
I need a dynamically allocated linked list Flex C Programming 2 03-06-2002 02:28 PM


All times are GMT -6. The time now is 09:22 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22