CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

strcat pointer question

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
e



Joined: 02 Feb 2005
Posts: 9
Location: New York City

View user's profile Send private message Visit poster's website

strcat pointer question
PostPosted: Wed Mar 22, 2006 4:43 pm     Reply with quote

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.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Mar 22, 2006 4:59 pm     Reply with quote

To append a single character which is extracted from another string,
you can use strncat() as shown below.

The program below displays:
Quote:
HelloE


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);
}
Guest








PostPosted: Thu Mar 23, 2006 6:28 pm     Reply with quote

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.
Code:
e



Joined: 02 Feb 2005
Posts: 9
Location: New York City

View user's profile Send private message Visit poster's website

PostPosted: Thu Mar 23, 2006 6:30 pm     Reply with quote

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);
   // ...
}
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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