-1

I am trying to pass the address of strings_line_tokens to split_string. I would think I would need the "&" for the address then one of these methods would work.

static void split_string(const char *buffer, size_t buflen, char ***strings_line_tokens)
static void split_string(const char *buffer, size_t buflen, char    **strings_line_tokens)
static void split_string(const char *buffer, size_t buflen, char **strings_line_tokens[])
static void split_string(const char *buffer, size_t buflen, char ***strings_line_tokens[])

Here is my declaration and where I try to pass the address to the function.

char *strings_line_tokens[503] = {0};
split_string(line, strlen(line)+1, &strings_line_tokens);

I keep getting some variation of this error.

warning: passing argument 3 of ‘split_string’ from incompatible pointer type main.c:73: note: expected ‘char ***’ but argument is of type ‘char * (*)[503]’

My goal after I properly pass the variable to my function is to do this. And after that I want to be able to use those values in main.

strings_line_tokens[*big_boy_counter] = malloc(strlen(ptr[i])+1);
strcpy(strings_line_tokens[*big_boy_counter], ptr[i]);
rockstar797
  • 137
  • 1
  • 2
  • 11
  • Can you please format the code properly? – Sourav Ghosh Oct 08 '15 at 23:19
  • Hmm, quite sure this is the first four-star code I've ever seen. Well, if being a three-star programmer is not a compliment, what would it be for a four-star programmer? And note that there is no "array of strings", but an "array of pointers to `char`" – too honest for this site Oct 08 '15 at 23:50
  • So, what do you expect of you pass a pointer to an array where a pointer to ... is expected? An array is not a pointer or vice versa. – too honest for this site Oct 08 '15 at 23:53
  • @Olaf thank you for clarifying "array of pointers to char". I always wanna use proper terminology when possible. Should the topic title of this post be changed? A LOT of people see that when the google for array of strings. http://stackoverflow.com/questions/1088622/how-do-i-create-an-array-of-strings-in-c – rockstar797 Oct 08 '15 at 23:57
  • Unsure. Problem is C does not have a proper string type, but some special semantics for a `char []`. That's why I always try to emphasise the diffference. Things become much easier once you see the index-operator as being overloaded for pointers (which it actually is). – too honest for this site Oct 09 '15 at 00:12
  • @naltipar how did you get the color in the code to show up? – rockstar797 Oct 10 '15 at 02:37
  • @SouravGhosh how do you get the color in the code to show up? – rockstar797 Oct 10 '15 at 02:38

2 Answers2

1

When you have

char *strings_line_tokens[503] = {0};

The type of &strings_line_tokens is char* (*)[503].
When you use string_line_tokens in a function call, it decays to type char**.

If you want to use the first form, you'll have to declare the function to be:

static void split_string(const char *buffer,
                         size_t buflen,
                         char* (*strings_line_tokens)[503]);
split_string(line, strlen(line)+1, &strings_line_tokens);
                              //  ^^^ Using the & operator.

If you want to use the second form, you'll have to declare the function to be:

static void split_string(const char *buffer,
                         size_t buflen,
                         char** strings_line_tokens);

split_string(line, strlen(line)+1, strings_line_tokens);
                               //  ^ Not using the & operator.

Take your pick.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • He does have a function prototype shown in the question for the 2nd form, but it isn't working. – clearlight Oct 08 '15 at 23:32
  • I have a question about the second one. My understanding with pointers is if I do not pass the address of `strings_line_tokens` then the values that got changed in split_string will be lost after it leaves the function. Main() will not be able to see what is changed. Is that correct? Or is this a really advanced usage of pointers? – rockstar797 Oct 08 '15 at 23:51
  • @rockstar797, that is partially correct. In `split_string`, if you change what `strings_line_tokens` point to, then the change won't be visible in `main`. However, if you change the elements of `strings_line_tokens`, the change will be visible in `main`. – R Sahu Oct 08 '15 at 23:53
  • @rockstar797: This is not advance usage, but missuse. You should overhault your concept once you encounter thress or more stars somewhere. At least as long you still have problems with that. – too honest for this site Oct 08 '15 at 23:55
  • @RSahu my goal is to add several elements to `strings_line_tokens`. So in this case the second one will be ok? – rockstar797 Oct 09 '15 at 00:04
  • @rockstar797, yes, that will be fine. – R Sahu Oct 09 '15 at 00:07
  • @RSahu the second method worked but I couldn't see the changes in my main(). All the values got lost. Glad I initialized everything to NULL or that would have been a pain to track down. And the first method gave a REALLY nasty error :(. `main.c: In function ‘split_string’: main.c:249: error: incompatible types when assigning to type ‘char *[503]’ from type ‘void *’ main.c:250: warning: passing argument 1 of ‘strcpy’ from incompatible pointer type /usr/include/string.h:128: note: expected ‘char * __restrict__’ but argument is of type ‘char **’ ` – rockstar797 Oct 10 '15 at 02:45
  • @rockstar797, my suggestion at this time for you would to be to ask a new question with an [MCVE](http://stackoverflow.com/help/mcve) and point out where you see the problems. Adding a link to the current question will add some context to the new question. – R Sahu Oct 10 '15 at 03:13
-1

These both work. I'd need to see your functions to know more about which to use and what might be better:

split_string(line, strlen(line)+1, (char ***)&strings_line_tokens);


split_string(line, strlen(line)+1, (char **)strings_line_tokens);
clearlight
  • 12,255
  • 11
  • 57
  • 75
  • `(char ***)&strings_line_tokens` is wrong; `(char **)strings_line_tokens` is redundant. – user253751 Oct 09 '15 at 00:06
  • Again, it comes down to use. I made them compile. – clearlight Oct 09 '15 at 00:25
  • Not everyone who posts posts enough code for an optimal answer. My goal was to show how to make the code compile in this case. To come up with something appropriate to the posters code, I would need to see the code (the functions being called and how the arguments were used). – clearlight Oct 09 '15 at 05:52
  • @nerdistcolony my is to do this in my function `strings_line_tokens[*big_boy_counter] = malloc(strlen(ptr[i])+1); strcpy(strings_line_tokens[*big_boy_counter], ptr[i]);`. Then have that returned to main. Sorry for not making that clear. R Sahu second method but then I couldn't see any of the changes in main. – rockstar797 Oct 10 '15 at 02:48