Why is not the memory address of a string assigned to a pointer?

1

What I want to do is to return the memory address of the CADE string that I generate in the function and assign it to the CHAIN pointer. But when I execute it, I mark the following error and try to solve it but it still does not work. ERROR: invalid conversion from char' to char * '

From what I understand it is because I can not assign it since one is char and the other is a pointer to a char. But how is the solution for you to do what I explained before? Thank you.

#include <stdio.h>

char * ingresa_cadena();





int main()
{ 
char *cadena;
cadena=ingresa_cadena();
printf ("La cadena ingresada es %c", cadena);




system("PAUSE");
return 0;
}

char * ingresa_cadena()
{ 
 char cade[25];
 gets(cade);
 return (&cade); }
    
asked by Maca Igoillo 30.01.2017 в 19:41
source

1 answer

2

Your code has several flaws:

char *cadena;
...
printf ("La cadena ingresada es %c", cadena);

You declare cadena as a pointer , but then you try to print a character .

You have 2 solutions:

Print the entire string:

printf ("La cadena ingresada es %s", cadena);
//                               ^s

Print a character (the first):

printf ("La cadena ingresada es %c", *cadena);
//                                   ^*

Apart from that, you do:

char * ingresa_cadena( ) { 
  char cade[25];
  gets(cade);
  return (&cade);
}

With that, you're returning a local fix to the function . In order not to repeat myself, I suggest you consult my answer to a previous question.

    
answered by 30.01.2017 / 19:53
source