How to assign string of one variable to other variable?

瘦欲@ 提交于 2020-01-11 14:05:16

问题


This is my first question on this site.

How do i assign string of one variable to other variable. What am i doing wrong here?

#include<stdio.h>
#include<string.h>
main(){

char a[30],b[30];

scanf("%s",a);
b[30]=a[30];
printf("%s",b);

}


回答1:


Use the standard C function strcpy declared in the header <string.h>. For example

strcpy( b, a );

Arrays do not have the assignment operator.

As for your statement

b[30]=a[30];

then b[30] and a[30] are undefined objects of the type char that are beyond the arrays.




回答2:


#include<stdio.h>
#include<string.h>
main(){

char a[30],b[30];

scanf("%s", a);
strcpy(b, a);          //header file <string.h>
                       //strcpy(destination, source)
printf("%s",b);

}

The strcpy() function will copy the content of string a in string b.



来源:https://stackoverflow.com/questions/58891532/how-to-assign-string-of-one-variable-to-other-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!