Interested in the possibility of transferring an array of arrays of characters (strings) to a function with the ability to change the values ​​of these strings globally.

#include <stdio.h> #include <string.h> void get(char ***s) { //пробовал еще (char **s[2]) *s[0] = "123"; return; } int main() { char *s[] = { "abc", "def" }; get(&s); printf("%s", s[0]); return 0; } 

Logically, everything should work - if we just pass the number a , then void get(int *a) , the array of characters get(char **s) , but with the array of strings it is no longer possible. How can I solve this problem?

  • 2
    Keep it simple - void get(char *s[]) { s[0] = "123"; return; } void get(char *s[]) { s[0] = "123"; return; } void get(char *s[]) { s[0] = "123"; return; } , describe what you are actually creating int main() { char *s[] = { "abc", "def" }; get(s); printf("%s", s[0]); return 0; } int main() { char *s[] = { "abc", "def" }; get(s); printf("%s", s[0]); return 0; } int main() { char *s[] = { "abc", "def" }; get(s); printf("%s", s[0]); return 0; } (note, replaced &s by s in the get() call) and everything will work (KISS principle) - avp
  • Thank you :) It turned out that everything worked for me too (no): it just showed that the reading error was in debug ... and when I started everything worked correctly - user277248
  • It may depend on the compiler, but I think the compiler should say that the type of the parameter in the function declaration void get(char ***s) does not match the type of the argument in the get(&s) call (for char *s[] = {...}; ) - avp

1 answer 1

The function you require is a pointer to a pointer to a pointer - you wrote an extra *. And then there is no need to transfer the function & s.