简体   繁体   中英

How to cast char to string in C?

Im new to C and i encounter a problem with using strcat(). I looked up strcat and noticed that it takes string as arguments but when i split items from a char array is a char and when i use it as a argument for strcat it prints out error.

#include <stdio.h>
#include <string.h>
int main()
{   
char a[10]="abcdefg123";
char *pa=a;
char *out[2]={"",""};
int counter=0;
while(*pa != '\0'){
    if (counter%2==0){
        strcat(out[0],*pa);
    }
    else{
    strcat(out[1],*pa);
    }

    counter++;
    pa++;    
   } 
printf("%s,%s\n",out[0],out[1]);

return 0;
}

the purpose of the code is to split a string into two string that each contain all the characters in odd position or even position.However, when im tring to dereference the char pointer all i get is a Char but it seems i need string

Use a temporary string in strcat instead of strcat(out[0],*pa); .

Also, make sure that you allocate enough memory for out .

int main()
{   
   char a[10]="abcdefg123";
   char temp[2] = {0};
   char *pa=a;

   // This is not good for `strcat`.
   // char *out[2]={"",""};

   // Use this instead.
   char out[2][20]={"",""};

   int counter=0;
   while(*pa != '\0'){
      temp[0] = *pa;
      if (counter%2==0){
         strcat(out[0], temp);
      }
      else{
         strcat(out[1], temp);
      }

      counter++;
      pa++;    
   } 
   printf("%s,%s\n",out[0],out[1]);

   return 0;
}

Point 1. As per the man page of strcat()

. .. the dest string must have enough space for the result.

Point 2. The second argument for strcat() is const char * , so the call cannot be strcat(out[0],*pa); . You need to use a temporary array for that, which will hold only the value *pa . Otherwise, you can make use of strncat() like

 strncat(out[0], pa, 1);  // copy only 1 char

In later case, you don't need to have any temporary array.

Reference: From man page, again,

The strncat() function is similar, except that it will use at most n bytes from src and src does not need to be null-terminated if it contains n or more bytes.

strcat() requires a pointer to a null-terminated string. You are dereferencing a pointer (with the * operator) which gives a char. You cannot simply cast a char to a pointer to sting. You might want to be using memcpy() instead of strcat(), but for copying single bytes a simple assignment using * operators on both the left and right sides would be fine. But as others have pointed out your code isn't allocating space for you to copy the chars into, so you're going to need to make additional changes to fix that. Also, you'll have to remember to copy a final null byte to the end of both your output strings.

In C, a "string" is really just a pointer to a character; the string runs from that character to the terminating 0 byte (the NUL ).

If you dereference a string pointer, you get the character at that position. If the pointer is pointing to the start of the string, you get the first character of the string.

Your program has some problems. For one, you need to allocate space for the new strings. strcat() will try to copy characters wherever you tell it to, but it's your job to make sure that there is room there and that it's okay to write there. The declaration of out just declares two pointers, and initializes them to point to a zero-length constant string. Instead, you need to allocate storage, something like:

char out0[64], out1[64];
char *out[]={out0, out1};

This makes two output buffers of 64 characters each, then sets up out with pointers to them.

Another problem: you declared a length of 10 for your char array, but then you initialize it with a length-10 string. This means there is no room for a terminating NUL byte and C won't put one. Then strcpy() or strcat() will copy extra garbage from the string, until there happens to be a NUL byte. If you are lucky there will be one right away and you won't spot the error, but if you aren't lucky you will get weird garbage.

Just let the compiler count how many bytes in your string and do the right thing. Leave out the length:

char a[]="abcdefg123";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM