一个字符串复制到一个字符数组 [英] Copy a string into a char array

查看:224
本文介绍了一个字符串复制到一个字符数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我想用户的输入拷贝到字符数组这是在一个结构中定义。诚恳我不知道如何做到这一点。

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
诠释的main(){
    员工结构
    {
        焦姓[11];
        焦姓[11];
        炭数[11];
        诠释工资;
    }    INT I;
    INT nemps;
    const int的maxemps = 5;
    员工结构* EMPS [maxemps]    的printf(你想存储多少员工也这样做(最大:5):);
    scanf函数(%D,nemps);    对于(i = 0; I< nemps;我++)
    {
        的printf(员工1./nLast的名字:);
        scanf函数(......)//读取字符串并将其复制成char姓氏[]
    }}


解决方案

首先结构员工* EMPS [maxemps]; 创建了的指针数组结构体员工的有数组大小的 maxemps 的。您还没有真正在内存中设置任何空间的实际结构,只是指针会指向他们。在堆上动态分配空间的结构,因此您可以以一种有意义的方式使用它们,你需要循环调用的malloc()像这样:

 为(i = 0; I< maxemps;我++){
   EMPS [I] =的malloc(sizeof的(结构员工));
}

您还需要在程序的最后一个类似的循环从而将免费()每个指针。

接下来,当你从一个用户使用输入你真的想使用与fgets() scanf()的,因为与fgets()允许您指定的字符数在阅读,因此您可以prevent溢出您的目的地缓冲区。

更新

如果你想通过声明一个或多个结构员工结构员工来工作,而无需使用这一步完成指针C $ C>在堆栈上,然后使用 成员访问运算符如下:

 结构员工EMP;
与fgets(emp.lastname,sizeof的(emp.lastname),标准输入);

UPDATE2

我发现在你的code的一些误区。请参阅 此链接 与评论工作示例。

Hello i want to copy the input of a user into an array of char which is defined in a struct. Sincerly i have no idea how to do this.

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


int main ()

{
    struct employee
    {
        char firstname[11];
        char lastname[11];
        char number[11];
        int salary;
    }

    int i;
    int nemps;
    const int maxemps = 5;
    struct employee* emps[maxemps];

    printf("How many employees do you want to store?(max:5): ");
    scanf("%d", nemps);

    for (i = 0; i < nemps; i++)
    {
        printf("Employee 1./nLast name:");
        scanf("....")// Read the string and copy it into char lastname[]
    }

}

解决方案

First off struct employee* emps[maxemps]; creates an array of pointers to struct employee with array size maxemps. You haven't actually set aside any space in memory for the actual structs, just the pointers that will point to them. To dynamically allocate space on the heap for your structs so you can use them in a meaningful way, you'll need to loop over a call to malloc() like so:

for (i = 0; i < maxemps; i++) {
   emps[i] = malloc(sizeof(struct employee));
}

You'll also want a similar loop at the end of your program which will free() each pointer.

Next, when you are getting input from a user you really want to be using fgets() over scanf() because fgets() allows you to specify the number of characters to read in so you can prevent overflows on your destination buffer.

Update

If you want to work with a single struct employee without the use of pointers this is accomplished by declaring one or more struct employee on the stack and then using the . member access operator as follows:

struct employee emp;
fgets(emp.lastname, sizeof(emp.lastname), stdin);

Update2

I found a number of errors in your code. Please see this link for a working sample with comments.

这篇关于一个字符串复制到一个字符数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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