Cannot convert 'char(*)[20]' to 'char*' in initialization.

#include <stdio.h>
#include <stdlib.h>
#include <ctime>

char location[5][20] = {"south with beaches", "mountains", "city", "country", "suburbs"};
char color[5][7] = {"blue", "red", "purple", "green", "orange"};
char food[5][12] = {"pizza", "pasta", "soup", "sandwiches", "steak"};
char car[5][7] = {"BMW", "Honda", "Toyota", "Ford", "Audi"};
char drink[5][18] = {"Coke", "Big Red", "Dr. Pepper", "Mountain Dew", "Water"};
char animal[5][8] = {"dog", "cat", "horse", "hamster", "turtle"};

char name[15];
int age;

int main()
{
srand(time(0));
char* Plocation = &location[rand()%5]; //[Error] cannot convert 'char (*)[20]' to 'char*' in initialization
char* Pcolor = &color[rand()%5]; //[Error] cannot convert 'char (*)[7]' to 'char*' in initialization
char* Pfood = &food[rand()%5]; //[Error] cannot convert 'char (*)[12]' to 'char*' in initialization
char* Pcar = &car[rand()%5]; //[Error] cannot convert 'char (*)[7]' to 'char*' in initialization
char* Pdrink = &drink[rand()%5]; //[Error] cannot convert 'char (*)[18]' to 'char*' in initialization
char* Panimal = &animal[rand()%5]; //[Error] cannot convert 'char (*)[8]' to 'char*' in initialization
printf("What is your name? ");
scanf("%s", &name);
printf("How old are you? ");
scanf("%d",&age);

printf("%s is %d years old. %s's favorite color is %s. %s likes to drive a %s. \nThey also like to drink %s with their %s. %s's favorite animal is a %s. \n%s would love to live in the %s.", name, age, name, Pcolor, name, Pcar, Pdrink, Pfood, name, Panimal, name, Plocation);

}
Last edited on
char* Plocation = location[rand()%5]; //try this.

I didn't explain last night, was tired...
you are taking an address of a pointer and that is one too many pointers. I took out the address of (&) and its effectively a pointer again, so it should work.
Last edited on
Topic archived. No new replies allowed.