Zahra19977
Zahra19977

Reputation: 355

Unable to assign a pointer to character to array of chararacters

I need to assign a pointer to an array of characters:

char str1[17];
char* h="hey";
str1 = h;

But i get this error : error C2440: '=' : cannot convert from 'char *' to 'char [17]'.

I was thinking that an array name is the same as a pointer to a character and array name is the address of the first character of the array so assigning a pointer to a char to array name would work.

I don't know why i get the error.

How can i fix it?

Upvotes: 2

Views: 58

Answers (1)

NPE
NPE

Reputation: 500177

You cannot make str1 point to a different address.

What you can do is use strncpy to copy the contents of h into str1.

Upvotes: 3

Related Questions