The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
building a string
Discuss building a string in the C Programming forum on Dev Shed. building a string 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:
|
|
|

July 15th, 2002, 03:29 AM
|
|
Junior Member
|
|
Join Date: Jul 2002
Posts: 0
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
building a string
i have a function which returns bytes of data 1 at a time.
i want to concatenate that bytes together to get a string
like the gets function does, only i don't get this input from the keyboard
it sounds easy, but i am new to the c language, so i would like to know how to do this
thank you
andre
|

July 15th, 2002, 11:56 AM
|
|
Contributing User
|
|
Join Date: Oct 2000
Location: Back in the real world.
|
|
|
i donīt think there is a pre-made function. you probably need to invent one like this:
void addToString(char *s, char c) {
int len;
len=strlen(s);
(s+len)^=c;
(s+len+1)^=0;
}
this one requires that you allocated enough memory for s before.
|

July 15th, 2002, 01:39 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
M. Hirsch, the code you posted is conceptually correct, but it looks like you are confusing pascal and c pointer syntax there  . Seriously though, there is a function called strcat, which concatenates two strings (rather than a string and a byte). Maybe you can do something like this:
Code:
void addToString(char *s, char c) {
char tmp[2];
tmp[0] = c; tmp[1] = '\0';
strcat(s, tmp);
}
However, the big issue with the above (the same is true for M. Hirsch's code as well) is that the length of the string is evaluated each time and an extra char is then tacked on to the end. For a long string, this might be a wasteful operation to determine the string length each time. A better way to do this IMHO would be something like this:
Code:
char *ptr;
char ch;
ptr = char_str; // char_str is assumed to be the string array that was allocated previously
while (GetByte(&ch)) {
*ptr = ch;
ptr++;
}
*ptr = '\0';
where GetByte() is assumed to be your function that returns a byte at a time. The above code does not recompute the string length each time and is therefore faster.
|

July 15th, 2002, 05:25 PM
|
|
Contributing User
|
|
Join Date: Oct 2000
Location: Back in the real world.
|
|
got me  iīm a delphi phreak
seems like i need some practise in C...
|
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
|
|
|
|
|