简体   繁体   中英

How to assign string of one variable to other variable?

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);

}

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.

#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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM