Doubt char char C

0

Hello. I need to make a pointer char point to a memory location that holds a phrase, in C.

I'm doing this:

char *ptr;
char array[3] = {'o','l','a'};
ptr = &array;

But I do not understand what's wrong, I also do not know how to program in C. Can anyone help? And I can not use the string.h library.

    
asked by anonymous 09.10.2017 / 22:55

2 answers

1

Try this:

char *ptr = "Ola";
    
09.10.2017 / 23:00
1

In C / C ++, the array name is a pointer to the first array element, so you can do the following:

ptr = array;

Another thing, if you want to save a string in an array of characters, do not forget the '%code%' terminator, otherwise things you do not expect can happen.

char *ptr;
char array[] = {'o','l','a', '
ptr = array;
'}; char outroArray[] = "Ola"; // também funciona ptr = array;
    
09.10.2017 / 23:31