如何使用C中的数组,函数和结构存储多个值? [英] How do I store multiple values using arrays, functions and structs in C?

查看:63
本文介绍了如何使用C中的数组,函数和结构存储多个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

初始化struct数组中的数据没有问题。



但是如果我想存储第一个结构记录中的First和第二个struct记录中的Next,程序会显示一堆随机数据。



这不是我想要的。



目前程序显示:



名称1:Init

名称2:Init

名称3:Init etc..ect ..



而不是我想要它存储和显示的是:



名称1:鲍勃

名字2:爱丽丝

姓名3:约翰等等..



我尝试过的事情:



I have no issue with initializing the data in a struct array.

However if I want to store say "First" in the first struct record and "Next" in the second struct record the program displays a bunch of random data.

Which is NOT what I want it to do.

At present the program displays:

Name 1 : Init
Name 2 : Init
Name 3 : Init etc..ect..

Instead what I want it to store and display is:

Name 1 : Bob
Name 2 : Alice
Name 3 : John etc.. etc..

What I have tried:

#include <stdio.h>
#define SIZE 2

struct date
{
    int day;
    int month;
    int year;
};

struct record
{
    char first_name[11];
    char surname[21];
    struct date DOB;
    int height;
    char eye_color;
    int weight;
};

void init_struct(struct record *ptr);
void display_struct(struct record data[SIZE]);

int main(void)
{
    struct record data[SIZE], *ptr = &data;

    init_data(ptr);
    display_data(data);

    return 0;
}

void display_data(struct record data[SIZE])
{
    int i;
    for(i = 0; i < SIZE; i++)
    {
        printf("Details of struct %d:\n\n", i + 1);
        printf("First name %d: %s\n", i + 1, data[i].first_name);
        printf("Surname %d: %s\n", i + 1, data[i].surname);
    }
}

void init_data(struct record *ptr)
{
    int i, j;
    for(i = 0; i < SIZE; i++)
    {
        printf("Enter details for struct %d\n\n", i + 1);
        printf("First name: ");
        scanf("%10s", (*(ptr + i)).first_name);
        printf("Surname: ");
        scanf("%20s", (*(ptr + i)).surname);
    }
}

推荐答案

对于初学者来说,在许多系统上根本无法编译:你是缺少一些前瞻性声明。你还应该包含对 string.h的引用

然后尝试更改你的作业:

For starters, that won't compile at all on many systems: you are lacking some of the forward declarations. You should also include a reference to string.h
Then try changing your assignment:
ptr = &data;



To

ptr = data;



作为整个程序,这应该有效:


As a "whole program", this should work:

#include <stdio.h>
#include <string.h>
#define SIZE 7

struct record
{
    char name[11];
};

void init_struct(struct record *ptr);
void display_struct(struct record data[SIZE]);
void display_data(struct record data[SIZE]);
void init_data(struct record *ptr);

int main(void)
{
    struct record data[SIZE];
    init_data(data);
    display_data(data);

    return 0;
}

void display_data(struct record data[SIZE])
{
    int i;
    for(i = 0; i < SIZE; i++)
    {
        printf("Name %d : %s\n", i + 1, data[i].name);
    }
}

void init_data(struct record *ptr)
{
    int i;
    for(i = 0; i < SIZE; i++)
    {
        strcpy(ptr[i].name, "Init");
    }
}


你需要正确学习C,这本书是C的参考。

你也可以找到教程。

使用调试器也可以帮助你理解程序中的错误。



这里是链接由该语言的作者参考C语言书。

C编程语言 - 维基百科,免费的百科全书 [ ^ ]

https://hassanolity.files.wordpress.com/2013/11/the_c_programming_language_2.pdf [ ^ ]

http://www.ime.usp.br/~pf/Kernighan-Ritchie/C-Programming-Ebook.pdf [ ^ ]

-----

当你不理解你的代码在做什么或为什么它做它的作用时,答案是调试器

使用调试器来看看你的代码在做什么。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量,这是一个令人难以置信的学习工具。



调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]



调试器在这里向您展示您的代码在做什么你的任务是与它应该做的事情进行比较。

调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。
You need to learn C properly, this book is the reference for C.
You can also find tutorials.
Using the debugger may also help you to understand what is wrong in your program.

Here is links to references books on C by the authors of the language.
The C Programming Language - Wikipedia, the free encyclopedia[^]
https://hassanolity.files.wordpress.com/2013/11/the_c_programming_language_2.pdf[^]
http://www.ime.usp.br/~pf/Kernighan-Ritchie/C-Programming-Ebook.pdf[^]
-----
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于如何使用C中的数组,函数和结构存储多个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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