Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

append a character from an array to a char pointer

Ok, so I'm a person who usually writes Java/C++, and I've just started getting into writing C. I'm currently writing a lexical analyser, and I can't stand how strings work in C, since I can't perform string arithmetic. So here's my question:

char* buffer = "";
char* line = "hello, world";

int i;
for (i = 0; i < strlen(line); i++) {
    buffer += line[i];
}

How can I do that in C? Since the code above isn't valid C, how can I do something like that? Basically I'm looping though a string line, and I'm trying to append each character to the buffer string.

like image 805
metro-man Avatar asked Jul 15 '14 03:07

metro-man


People also ask

Can you append to a char array?

The append() method in the StringBuffer class adds the specified String to the contents of the current String buffer. Using this method you can append a single character to a string or char array in java.

How do I add a character to a string pointer?

The simplest solution, lacking any context, is to do: char buffer[ strlen(line) + 1 ]; strcpy(buffer, line); You may be used to using pointers for everything in Java (since non-primitive types in Java are actually more like shared pointers than anything else).

How do you append to a char array in C++?

C++ has a built-in method to concatenate strings. The strcat() method is used to concatenate strings in C++. The strcat() function takes char array as input and then concatenates the input values passed to the function. In the above example, we have declared two char arrays mainly str1 and str2 of size 100 characters.


3 Answers

string literals are immutable in C. Modifying one causes Undefined Behavior.

If you use a char array (your buffer) big enough to hold your characters, you can still modify its content :

#include <stdio.h>

int main(void) {


    char * line = "hello, world";
    char buffer[32]; // ok, this array is big enough for our operation

    int i;
    for (i = 0; i < strlen(line) + 1; i++) 
    {
        buffer[i] = line[i];
    }

    printf("buffer : %s", buffer);

    return 0;
}
like image 63
quantdev Avatar answered Sep 30 '22 21:09

quantdev


First off the buffer needs to have or exceed the length of the data being copied to it.

char a[length], b[] = "string";

Then the characters are copied to the buffer.

int i = 0;
while (i < length && b[i] != '\0') { a[i] = b[i]; i++; }
a[i] = '\0';

You can reverse the order if you need to, just start i at the smallest length value among the two strings, and decrement the value instead of increment. You can also use the heap, as others have suggested, ordinate towards an arbitrary or changing value of length. Furthermore, you can change up the snippet with pointers (and to give you a better idea of what is happening):

int i = 0;
char *j = a, *k = b;
while (j - a < length && *k) { *(j++) =  *(k++); }
*j = '\0';

Make sure to look up memcpy; and don't forget null terminators (oops).

like image 23
motoku Avatar answered Sep 30 '22 20:09

motoku


#include <string.h>

//...

char *line = "hello, world";
char *buffer = ( char * ) malloc( strlen( line ) + 1 );

strcpy( buffer, line );

Though in C string literals have types of non-const arrays it is better to declare pointers initialized by string literals with qualifier const:

const char *line = "hello, world";

String literals in C/C++ are immutable.

If you want to append characters then the code can look the following way (each character of line is appended to buffer in a loop)

#include <string.h>

//...

char *line = "hello, world";
char *buffer = ( char * ) malloc( strlen( line ) + 1 );

buffer[0] = '\0';
char *p = Buffer;

for ( size_t i = 0; i < strlen( line ); i++ )
{
    *p++ = line[i];
    *p = '\0';
}

The general approach is that you find the pointer to the terminating zero substitute it for the target character advance the pointer and appenf the new terminating zero. The source buffer shall be large enough to accomodate one more character.

like image 34
Vlad from Moscow Avatar answered Sep 30 '22 19:09

Vlad from Moscow