0

I'm beginner in the studies of C language (not C++) and I'm trying to define a dynamic array of strings, but I'm experiencing difficulties for appending an element.

I have tried to define the array as:

char **e = malloc(3 * sizeof(char *));
e[0] = "abc";
e[1] = "def";
e[2] = "ghi";

It ran successfully, but trying to resize it, using:

  **e = realloc(e, 4 * sizeof(char *));

Returned the error: "assignment makes integer from pointer without a cast". What am I doing wrong?

Thanks, Fábio

3
  • 4
    Get rid of the ** in your assignment: e = realloc(e, 4 * sizeof(char *)); Commented Mar 13, 2016 at 1:36
  • And check the return values of malloc() and realloc() before using them. Both functions can fail and, if they do, subsequent behaviour of code using the returned values will be undefined. Commented Mar 13, 2016 at 1:54
  • Fabio, did you get the info you needed? Are you going to accept an answer? Commented Mar 15, 2016 at 3:22

2 Answers 2

2

Short answer:

e = realloc(e, 4 * sizeof(char *));

Long answer:

Let's split your original declaration char **e = malloc(3 * sizeof(char *)); into declaration and assignment.

char **e;
e = malloc(3 * sizeof(char *));

The first line introduces the variable e and specifies its type char **. The second line assigns the expression that you put on the right side of the = sign to the newly declared variable.

To assign a new value to e, you modify the second line to read:

e = realloc(e, 4 * sizeof(char *));
Sign up to request clarification or add additional context in comments.

1 Comment

If realloc fails and returns NULL you lose the original array. Store the result of realloc in a temporary variable first.
0

This is the refined code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
        char **e = malloc(3 * sizeof(char *));
        e[0] = malloc(4 * sizeof(char));
        e[1] = malloc(4 * sizeof(char));
        e[2] = malloc(4 * sizeof(char));
        strcpy(e[0], "abc");
        strcpy(e[1], "def");
        strcpy(e[2], "ghi");
        puts(e[0]);
        puts(e[1]);
        puts(e[2]);
} 

Some problems found in your code:

  1. To allocate memory to a pointer, write ptr = malloc(1), not malloc(ptr, 1).

  2. e is a "pointer to pointer to char", while e[0] to e[2] are "pointers to char". So when mallocing e, use sizeof (char *), and when mallocing e[x]', usesizeof (char)` instead.

  3. *e = "abc"; only copy the address of the string literal, not the value it contains. To copy the whole char array, you have to write strcpy(e[0], "abc");.

2 Comments

Sun Qingyao, do we need, then, to allocate memory for every single element of the array?
@FábioSoldá No. For example, e[0] = malloc(4 * sizeof(char)); has allocated memory for all of its elements: e[0], e[1], e[2], e[3]

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.