![]() |
| | #1 |
| Registered User Join Date: Feb 2009
Posts: 19
| dynamically allocated strings?? Code: typedef struct
{
char *name;
char *number;
}myStruct;
Code: void printMyStruct(myStruct *a, myStruct *b)
{
printf("%s: %s\n", *(a).name, *(b).number);
}
|
| CS_Student8337 is offline | |
| | #2 |
| Resu Deretsiger Join Date: Nov 2008 Location: /dev/null
Posts: 185
| Use the malloc() function. For example: Code: char *string = malloc(sizeof(char) * 100); 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 | |
| | #3 |
| CSharpener 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 | |
| | #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 | |
| | #5 |
| Resu Deretsiger 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 | |
| | #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 | |
| | #7 |
| Resu Deretsiger 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 | |
| | #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 | |
| | #9 | |
| Registered User Join Date: Dec 2006 Location: Canada
Posts: 2,003
| Quote:
| |
| cyberfish is offline | |
| | #10 |
| Resu Deretsiger 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 | |
| | #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 | |
| | #12 |
| Resu Deretsiger Join Date: Nov 2008 Location: /dev/null
Posts: 185
| Like so . . . Code: a->name = localstring;
__________________ 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 | |
| | #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 | |
| | #14 |
| CSharpener Join Date: Oct 2006
Posts: 5,314
| 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 | |
| | #15 |
| CSharpener Join Date: Oct 2006
Posts: 5,314
| is a pointer or struct?
__________________ If I have eight hours for cutting wood, I spend six sharpening my axe. |
| vart is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |