0

Lets say I have a variable:

char** code;

I then do:

*code[0] = "Lucas"

Is it valid to say that, **code holds an array of pointers (*code is the array which I am making strings) and that *code[0] will equal "Lucas" and that *code[0][2] will equal 'c'?

Sorry if it seems elementary, I am getting very confused with double pointers! Thanks in advance!

-Lucas Giancola

4
  • 1
    You need to reserve space using malloc before, e.g: char **code = malloc(sizeof(*code) * n); and then code[0] = "Lucas" (without the *) Commented Feb 10, 2015 at 23:06
  • @AlterMann, You would probably get a warning/error for that because you'll be dropping the const from the string literal and will not have an array you can manipulate. Commented Feb 10, 2015 at 23:09
  • @BrianMcFarland, "Lucas" is a string literal (in a read-only segment), true, but there is nothing wrong in the assignment. Commented Feb 10, 2015 at 23:22
  • 2
    @BrianMcFarland: Not in C. String literals are of the type char* (actually char[N]), even though they're technically const char[]. This is an exception for mostly historical reasons, since C didn't always have const; it probably remains because a lot of existing code uses char* instead of const char*. Commented Feb 10, 2015 at 23:24

2 Answers 2

3

Is it valid to say that, **code holds an array of pointers

No it is not. You have allocated space for a single pointer. You have told the compiler that that pointer will point at another pointer (that you have not created yet) that points to a character (that you have not created yet either)

*code[0] = "Lucas"

Is not valid code and doesnt compile

prog.cpp:6:8: error: invalid conversion from 'const char*' to 'char' [-fpermissive]
  *f[0] = "Lucas";
Sign up to request clarification or add additional context in comments.

Comments

2

If you just have

char** code;
*code[0] = "Lucas";

you'll run into undefined behavior since you did not allocate any memory for code.

You'll need to use:

char** code = malloc(SOME_SIZE*sizeof(*code));

Even after that, using:

*code[0] = "Lucas";

is not good. There are couple of problems with that.

  1. code[0] is a pointer. *code[0] is that pointer dereferenced. If you already have some string in code[0], then, *code[0] will be first character of that string. The assignment is, therefore, wrong.

  2. Also, "Lucas" is going to be in a read-only parts of the compiled code. You will need to make a copy of "Lucas" using strdup before you assign it to a variable that is of type char *.

You need something like:

 code[0] = strdup("Lucas");

Now you can use code[0] to access the whole string. You can use *code[0] to access the first character of that string. You can also use code[0][0] to access the first character of that string.

1 Comment

Warning: strdup is not standard C. It is a POSIX extension. You can easily write one yourself, though, via the use of malloc, strlen and memcpy --- just remember to name it differently (in case you are in a POSIX system).

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.