简体   繁体   English

使用 strcpy() 为结构赋值。 程序不运行

[英]Using strcpy() to assign values to a struct. Program doesnt run

I am new to c and I'm trying to assign values to a struct using strcpy(I've tried to use assignment using lvalue method and that throws an error).我是 c 的新手,我正在尝试使用 strcpy 为结构赋值(我尝试使用左值方法使用赋值并引发错误)。 I try to run the program and nothing happens.我尝试运行该程序,但没有任何反应。 Where is the error in my code?我的代码中的错误在哪里? I'm using codeblocks to run the program.我正在使用代码块来运行程序。

#include <stdio.h>
#include <stdlib.h>

int main(){
    struct student{
        char name[25];
        int age;
    };
    struct student s1;
    strcpy(s1.name, "Sammy");
    strcpy(s1.age, "18");
    printf("%s, %d", s1.name, s1.age);
}

程序不运行

Remove this:删除这个:

strcpy(s1.age, "18");

You can to normal assignment.你可以正常赋值。 s1.age = 18;

The first thing to do is to read the warnings from your compiler as they contain valuable information about problems with your code.首先要做的是阅读来自编译器的警告,因为它们包含有关代码问题的宝贵信息。 Compiling the original code with gcc and clang gives these warnings:使用 gcc 和 clang 编译原始代码会给出以下警告:

clang:铛:

so5.c:12:12: warning: incompatible integer to pointer conversion passing 'int' to parameter of type 'char *' [-Wint-conversion] so5.c:12:12: 警告:不兼容的整数到指针转换将“int”传递给“char *”类型的参数 [-Wint-conversion]

gcc:海湾合作委员会:

so5.c:12:12: warning: passing argument 1 of 'strcpy' makes pointer from integer without a cast [-Wint-conversion] strcpy(s1.age, "18"); so5.c:12:12: 警告:传递参数 1 的 'strcpy' 使指针从整数而不进行强制转换 [-Wint-conversion] strcpy(s1.age, "18");

So the offending line is:所以违规行是:

strcpy(s1.age, "18");

is first treating the uninitiated s1.age as pointer and tries to copy the string into this unknown location.首先将未初始化的 s1.age 视为指针并尝试将字符串复制到这个未知位置。 This will very likely result in a segmenation fault.这很可能会导致分段错误。

You could of course make a pointer of this and fill its memory contents with the "18"你当然可以制作一个指针并用"18"填充它的内存内容

strcpy(&s1.age, "18");

But wait this results in a print like:但是等待这会导致打印如下:

Sammy, 14385萨米,14385

Because the string is encoded in ascii, three bytes 0x31,0x38,0因为字符串是用ascii编码的,三个字节0x31,0x38,0

When written into the location of the int age field as 0x003831 or in decimal 14385 .当写入int age字段的位置时为0x003831或十进制14385 This is however pure luck that the reaming byte(s) was randomly initiated to 0.然而,这纯粹是运气,铰孔字节被随机初始化为 0。

The correct way to assign age a value is with an assignment:为年龄赋值的正确方法是赋值:

s1.age = 18;

strcpy() is used for copying strings to a character buffer. strcpy()用于将字符串复制到字符缓冲区。 With strcpy(s1.age, "18");strcpy(s1.age, "18"); , you are trying to read something from the address of the string 18 and write to s1.age which is an int . ,您正在尝试从字符串18的地址读取某些内容并将其写入s1.age ,这是一个int

Either declare the age member to be a char array, just like you did for name (although this would restrict you from doing arithmetic operations on age ).要么将age成员声明为char数组,就像您对name所做的那样(尽管这会限制您对age进行算术运算)。 Or, use the assignment operator = to assign an int .或者,使用赋值运算符=分配一个int Like: s1.age = 18;比如: s1.age = 18;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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