Click here to Skip to main content
15,867,785 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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);
    }
}
Posted
Updated 3-Apr-17 9:46am
v3

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");
    }
}
 
Share this answer
 
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900