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;
}
Answer from R Sahu on Stack OverflowUse 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;
}
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.
C string, char and pointers - Stack Overflow
Does c have strings
Convert single character string to char - C++ Forum
How do I convert a string to an array of chars in C?
Videos
A short answer won't do your question justice. You're asking about two of the most significant, but often misunderstood, aspects of C.
First of all, in C, by definition, a string is an array of characters, terminated with a nul character, '\0'.
So if you say
char string1[] = "Hello";
it's as if you had said
char string1[] = {'H', 'e', 'l', 'l', 'o', '\0'};
When you use a string constant like "Hello" in your program, the compiler automatically constructs the array for you, as a convenience.
Next we have the character pointer type, char *. You called this a "string to a char array", but it's really a pointer to a single char. People often refer to char * as being C's "string type", and in a way it is, but it can be a misleading thing to say, because not every char * is a string, and more importantly C has very few mechanisms for automatically managing strings for you. (What I mean is that, in C, you can't just sling strings around as if they were a basic data type, like you can in C++ or BASIC. You typically have to worry about where they're stored, how they're allocated.)
But let's go back to the definition of a string. If a string is an array of characters, then why would a pointer to characters be useful for manipulating strings at all? And the answer is, pointers are always useful for manipulating arrays in C; pointers are so good at manipulating arrays that sometimes it seems as if they are arrays. (But don't worry, they're not.)
I can't delve into a full treatise on the relationship between arrays and pointers in C here, but a couple of points must be made:
If you say something like
char *string2 = "world";
what actually happens (what the compiler does for you automatically) is as if you had written
static char __temporaryarray[] = "world";
char *string2 = __temporaryarray;
And, actually, since string2 is a pointer to a character (that is, what it points at is characters, not whole arrays of characters), it's really:
char *string2 = &__temporaryarray[0];
(That is, the pointer actually points to the array's first element.)
So since a string is always an array, when you mentioned the string "world" in a context where you weren't using it to initialize an array, C went ahead and created the array for you, and then made the pointer point to it. C does this whenever you have a string constant lying around in your program. So you can later say
string2 = "sailor";
and you can also say things like
if(strcmp(string1, "Goodbye") == 0) { ... }
Just don't do
char *string3;
strcpy(string3, "Rutabaga"); /* WRONG!! *//
That's very wrong, and won't work reliably at all, because nobody allocates any memory for string3 to point to, for strcpy to copy the string into.
So to answer your question, you will sometimes see strings created or manipulated as if they are arrays, because that's what they actually are. But you will often see them manipulated using pointers (char *), because this can be extremely convenient, as long as you know what you are doing.
Why do I see some books using the pointers method while others use the normal definitions ?
Neither of those is more "normal" than the other. A string in C is purely a contiguous (uninterrupted) series of char terminated with a 0 char. You can refer to that with a char[], or you can refer to it with a char *, or a const char *, etc. They're all just ways of referring to the block of memory with the series of chars in it. Each is appropriate in different contexts, depending on how the code in question receives the string and what it's doing with it.
My friends are spilt down the middle on this. Half of us think since a c doesn’t have built in strings and only arrays of characters that they don’t. While the other half think that the array of characters would be considered string.
I know that a string is already technically an array of chars, but when I try to use toupper(string), it doesn’t work because toupper is designed to capitalize chars and not strings, per the documentation. I’ve been making it overly complicated and it’s stressing me out. So to start, I created an “int N=strlen(string);”, then created an array that’s “char upper[N];”. Then I write a for loop written as(please forgive the terrible syntax I’m about to write), “for (int i = 0; i < N; i++) { toupper(upper[j]); }”. What am I doing wrong?
In C, a string is actually stored as an array of characters, so the 'string pointer' is pointing to the first character. For instance,
char myString[] = "This is some text";
You can access any character as a simple char by using myString as an array, thus:
char myChar = myString[6];
printf("%c\n", myChar); // Prints s
Hope this helps! David
In C, there's no (real, distinct type of) strings. Every C "string" is an array of chars, zero terminated.
Therefore, to extract a character c at index i from string your_string, just use
char c = your_string[i];
Index is base 0 (first character is your_string[0], second is your_string[1]...).