0

I need to append a array inside a linked list with a single character of an array also inside a linked list. I tried the strcat function but it didn't work.

for(int k=0; k < strlen(Node2->array); k++){
      strcat((char *)Node->array, (char *)Node2->array[k]);
}

The ide gives this warning

cast to pointer from integer of different size -wint-to-pointer-cast

and I when I run the program the program stops, it can't go on.

1
  • 2
    "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." Commented Jan 11, 2018 at 3:39

1 Answer 1

1

There's no means to append to an array in C, as arrays are, per definition fixed (at compile time) lists of same type elements stored contiguously. Only if you have enough array size, a string (which is some char array which is partially filled by appending a \0 null character at the end of the valid char list) can be appended by simply moving the \0 char one place further (if there's place in the array for a character more) and putting before it the desired char. An example will illustrate this (I'll somewhat simplify to separate the list problem you point that has nothing to do with the appending problem)

Let's have:

char c = 'A';
char array[100] = "The string to be appended with ";

The second variable is an array initialized with the contents of the string literal indicated, plus \0 chars to complete array size until filling the 100 characters space of the array.

Several ways to append the contents of c variable are:

/* check first that array has space to store data */
int l = strlen(array);
if (l + 1 < sizeof array) {
    array[l] = c;
    array[l+1] = '\0';
}

Another way (if you don't need to know if the operation succeeded and only append the character if space is allocated) is to snprintf(3) the char at the end of the string value (the snprintf(3) controls correctly the space)

int l = strlen(array); /* number of chars in string */
snprintf(array + l,  /* char pointer at string end */ 
    sizeof array - l, /* the number of chars of space available */
    "%c",  /* format string to print a character */
    c);  /* data character to append */

By the way, the return value of snprintf(3) is the number of characters printed, so it should be 1 in case it worked successfully, and 0 in case there's no space left in the array to print the c variable and the final \0 character, so you can check the result of the operation.

NOTE

If you have a char * pointer in the node structure, because you allocated dynamically the memory to store the string value (for example, using strdup(3) function) then you have no space to append any character at the end as strdup(3) only allocates enough space to store a full copy (the characters, plus the extra \0 at the end) of the original string (even if that string was originally stored in a bigger character array) In this case, the only way to append a character is to allocate a new array with enough size for the new length and copy all the characters to the new place. Be careful, that if you do this in a per character basis, you'll get efficiency severely affected.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.