Computer Science Canada

variable length strings

Author:  Fonzie [ Sat Sep 29, 2007 6:23 pm ]
Post subject:  variable length strings

I'm continuously finding that things I took for granted in other languages are very difficult and frustrating in C. Anyway, the problem is I need a variable length string. I have a loop that checks a passed character, and if it is deemed valid, it concatenates it and the "total" string. so through this loop I have to keep overwriting the total string and replacing it with total string + valid passed character. I believe this is causing a segmentation fault though. Is there anyway to make this work?

Author:  Saad [ Sat Sep 29, 2007 6:27 pm ]
Post subject:  RE:variable length strings

Some code would be useful.

Author:  haskell [ Sat Sep 29, 2007 7:50 pm ]
Post subject:  RE:variable length strings

I'd recommend looking into malloc and free.

Author:  wtd [ Sat Sep 29, 2007 10:04 pm ]
Post subject:  RE:variable length strings

Yes.

A) Strings in C are just arrays of characters.
B) Arrays in C are just blocks of memory.
C) Blocks of memory can be allocated at run-time.

Author:  md [ Sat Sep 29, 2007 10:55 pm ]
Post subject:  RE:variable length strings

Not so! Currently I am working on a program (of sorts) that cannot allocate memory at runtime, mostly because I haven't gotten around to writing the code to do that Wink

Another solution is to have a buffer that's as big as the biggest 'total' string you'll accept; just allocate it on the stack and you're good to go.

Author:  Fonzie [ Sun Sep 30, 2007 12:21 am ]
Post subject:  Re: variable length strings

char total[10] = "abc";
total[10] = strcat(total, 'a');

doesn't this code cause a segmentation fault in C? And if so, how am I supposed to continuously concatenate single letters to total?

Author:  Saad [ Sun Sep 30, 2007 7:54 am ]
Post subject:  RE:variable length strings

Yes, because in the code you create an array which has a size of 10, and since arrays start indexing at 0, the available range is 0-9, and you try accessing 10 which is not allocated, and also strcat returns a pointer to the final string(or array) so all you need is
total = strcat(total,'a');
but keep in mind you can overflow the array size

Author:  haskell [ Sun Sep 30, 2007 7:58 am ]
Post subject:  RE:variable length strings

http://en.wikipedia.org/wiki/Malloc

Author:  md [ Sun Sep 30, 2007 7:24 pm ]
Post subject:  Re: variable length strings

Fonzie @ 2007-09-30, 12:21 am wrote:
char total[10] = "abc";
total[10] = strcat(total, 'a');

doesn't this code cause a segmentation fault in C? And if so, how am I supposed to continuously concatenate single letters to total?


This should actualy cause a compiler error since strcat returns a char* and you're assigning the result to a char. If you fix that then you're still passing a character to strcar when it expects a character pointer. If you somehow fix THAT, say by declaring char a = 'a' and passing a pointer to it instead, then you're left with the problem of not passing a null terminated string.

Basically what I'm saying is that that code is so far from being even close to good. That being said; here is some proper code:

C:

char total[10] = "abc";
char extra[] = "d";

strcat(total, extra);


after that total would be equal to "abcd"


: