assigning a string to another string
Posted
by
user1509676
on Stack Overflow
See other posts from Stack Overflow
or by user1509676
Published on 2012-07-08T07:39:34Z
Indexed on
2012/07/08
9:15 UTC
Read the original article
Hit count: 133
c
Why this code is not running? Why str1 is not assigned to str2 ?? I know i have an option of using strcpy but i wish to know the reason why this is not working??
#include<stdio.h>
int main()
{
char str1[]="hello";
char str2[10];
str2=str1;
printf("%s",str2);
return 0;
}
Whereas if I use pointers than it works like here..
#include<stdio.h>
int main()
(
char *s="good morning";
char *q;
q=s;
while(*q!='\0')
{
printf("%c",*q);
q++;
}
return 0;
}
This works. Now the string has been copied via pointers so why such difference??
© Stack Overflow or respective owner