-1
#include <stdio.h>
#include <stdlib.h>
char pointer();

void main() {
    char*name = "Paz Leviim", **p_name = &name;
    *p_name = pointer();
    printf("Now it's %s", *p_name);
    getchar();
}
char pointer() {
    char name_to_main[100];
    printf("The value now is Paz Leviim\nPlease set value:");
    scanf_s("%[^\n]%*c", &name_to_main,100);
    return name_to_main;
}

How can I return the value of variable name_to_main to the pointer *p_name?

Paz
  • 23
  • 1
  • 7
  • 1
    Pass an array (and its size) as *arguments* to the function, and let it fill in that array. – Some programmer dude Nov 25 '17 at 16:51
  • 3
    char != char* , and main() should return int , not void. – wildplasser Nov 25 '17 at 16:51
  • 1
    You can't. Either define `name_to_main[]` in `main()` and pass as an argument, or use `malloc()` to allocate storage within `pointer()`. And consider a more meaningful name for the function `pointer()`. – ad absurdum Nov 25 '17 at 16:52
  • 1
    Possible duplicate of [How to make an array return type from C function?](https://stackoverflow.com/questions/14297169/how-to-make-an-array-return-type-from-c-function) – Ken Y-N Nov 25 '17 at 16:53
  • What does strcpy do? What does strdup do? – n. m. could be an AI Nov 25 '17 at 16:54
  • You could return a struct that embeds an array - if it's not allocated on the stack (wel, you can still return it but that's UB). – Déjà vu Nov 25 '17 at 16:55
  • 2
    I often wonder why sometimes we see things like `"%[^\n]%*c"`, which I can't decipher, alongside fundamental beginner errors like in the rest of the function. – Ken Y-N Nov 25 '17 at 16:55
  • You should also change `&name_to_main` to `name_to_main`, since arrays decay to pointers to their first elements in most expressions, including function calls. – ad absurdum Nov 25 '17 at 16:56
  • 1
    @RingØ -- note that you can define a `struct` wrapping the array _within_ the function, and return it. The value will be copied to the receiving variable (which must be a compatible `struct`). – ad absurdum Nov 25 '17 at 16:58
  • I've removed the & in the name_to_main and still not working – Paz Nov 25 '17 at 16:59
  • @Paz -- that was only one of a very large number of mistakes. [You should get a good book](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) to learn from. – ad absurdum Nov 25 '17 at 17:03
  • @DavidBowling Totally right! It's actually quite difficult to return something stack-allocated from a struct, as it's returned by value! – Déjà vu Nov 25 '17 at 17:05
  • @KenY-N, it's the result of copy & paste coding. The OP probably doesn't understand that in any detail either, but they found it on the Internet -- quite possibly even right here on SO. – John Bollinger Nov 25 '17 at 17:08
  • In fact, looking at the arguments to that `scanf_s()` call, I'm *confident* that the OP does not fully understand it. – John Bollinger Nov 25 '17 at 17:09
  • @Paz The simplest solution is just to change it to `static char name_to_main[100];`. This is not a perfect solution, and it has several drawbacks, but it should get you started. – Steve Summit Nov 25 '17 at 17:13
  • I know that `static` in most cases can be useful but in this case I've got again the error – Paz Nov 25 '17 at 17:29
  • Again, @Paz -- threre are _many_ errors in your code, and we are not a debugging service. That said: `char pointer();` --> `char * pointer(void);`, and `void main() {}` --> `int main(void) {}` should help. – ad absurdum Nov 25 '17 at 17:42
  • Now my code it's works perfect :) – Paz Nov 25 '17 at 22:58
  • Thanks for everyone here I was should to do only `static char name_to_main[100]` and `char*pointer()` – Paz Nov 25 '17 at 22:59

1 Answers1

0
#include <stdio.h>
#include <stdlib.h>
char*pointer(char*now);

void main() {
    char*name = "Paz Leviim", **p_name = &name;
    *p_name = pointer(*p_name);
    printf("Now it's %s", *p_name);
    getchar();
}
char*pointer(char*now) {
    static char name_to_main[100];
    printf("The value now is %s\nPlease set value:",now);
    scanf_s("%[^\n]%*c", &name_to_main,100);
    return name_to_main;
}

Now all working :)

Paz
  • 23
  • 1
  • 7