![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
e
Joined: 02 Feb 2005 Posts: 9 Location: New York City
|
strcat pointer question |
Posted: Wed Mar 22, 2006 4:43 pm |
|
|
hi, still learning pointers - can someone explain how to use strcat() to append a single character?
Code: | strcat(stringA, stringB[i]); | doesn't work b/c compiler expects pointer, not a single char at index i.
i tried things like
Code: | strcat(stringA, &stringB[i]); | but i still get "Previous identifier must be a pointer" error. setting a pointer variable first:
Code: | char *c;
c = &stringB[i];
strcat(stringA, c); | also doesn't work.
thanks. |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Mar 22, 2006 4:59 pm |
|
|
To append a single character which is extracted from another string,
you can use strncat() as shown below.
The program below displays:
Also, we don't normally use "c" as a variable name for a pointer.
"c" is used for a character variable. A generic temporary pointer
is usually called "ptr".
Code: | #include <16F877.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#include <string.h>
//============================
void main()
{
char stringA[20] = {"Hello"};
char stringB[27] = {"ABCDEFGHIJKLMNOPQRSTUVWXYZ"};
char *ptr;
int i;
i = 4;
ptr = &stringB[i];
strncat(stringA, ptr, 1);
printf("%s\n\r", stringA);
while(1);
} |
|
|
![](templates/subSilver/images/spacer.gif) |
Guest
|
|
Posted: Thu Mar 23, 2006 6:28 pm |
|
|
thanks for the quick reply. i didn't see strncat in my manual, that is useful. as it turns out, the error i was getting was due to my passing a string parameter incorrectly. but you are right, i also needed str<b>n</b>cat because i wanted only 1 char. i was confused initally because passing a string name works the same as pointer to that string, but doesn't work in functions like strcat.
Code: |
void displayString(char string) {
printf("%s", string);
}
//works the same as:
void displayString(char *string_ptr) {
printf("%s", string_ptr);
}
void main() {
char s[6];
char text[6] = "hello";
strcpy(s, text);
displayString(s);
// ...
}
|
but the first routine does not work with strcat because it expects a pointer. i thought the reference 'string' was the equivalent to a pointer to the first char in the string array. which i think it is, so i'm a still bit confused. |
|
![](templates/subSilver/images/spacer.gif) |
e
Joined: 02 Feb 2005 Posts: 9 Location: New York City
|
|
Posted: Thu Mar 23, 2006 6:30 pm |
|
|
that was me ('e'), forum logged me out. to clarify:
Code: |
char string2[7] = " there";
void displayString(char string) {
strcat(string, string2); // *compiler ERROR for this line*
printf("%s", string); // ...but this works (by itself)!
}
//works the same as:
void displayString(char *string_ptr) {
strcat(string_ptr, string2); // this works
printf("%s", string_ptr);
}
void main() {
char s[6];
char text[6] = "hello";
strcpy(s, text);
displayString(s);
// ...
} |
|
|
![](templates/subSilver/images/spacer.gif) |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|