如何将一个变量的字符串分配给另一变量? [英] How to assign string of one variable to other variable?

查看:139
本文介绍了如何将一个变量的字符串分配给另一变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我对此网站的第一个问题.

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

}

推荐答案

使用标头<string.h>中声明的标准C函数strcpy.例如

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

strcpy( b, a );

数组没有赋值运算符.

至于你的声明

b[30]=a[30];

然后b[30]a[30]是超出数组的char类型的未定义对象.

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

这篇关于如何将一个变量的字符串分配给另一变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆