Click here to Skip to main content
15,881,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is the logic that I used in my other code, but it's not working. I'm not familiar with pointers but I saw some examples and came up with this.

What I have tried:

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

struct People
{
   int id;
   char name[20];
   int age;
};

void insert(struct People *array[], int i)
{
    //&array[i]->id = &array[i]->id + 1; //im trying to increment id for every person
    printf("\nInsert name of %d. person: ", i);
    scanf("%s", &array[i]->name);
    printf("Insert age of %d. person: ", i);
    scanf("%d", &array[i]->age);
}

int main()
{
    int i = 0, add;
    struct People array[100];

    printf("\nType 1 to add a new person ");
    scanf("%d", &add);
    while(add==1)
    {
        i++;
        insert(&array, i);
        printf("\nType 1 to add a new person ");
        scanf("%d", &add);
    }
}
Posted
Updated 27-Dec-21 23:57pm
Comments
k5054 28-Dec-21 5:22am    
"but it's not working" doesn't help much. What goes wrong? Does the program compile? If it does, what happens when you run it? Does it crash, or do you get unexpected results?

Don't do it like that: the Insert method doesn't need to "know" about how a collection of People objects is stored - it's only concerned with a single People (and to make it more readable, call that class "Person" to show it only holds one set of information).

And the Insert function doesn't "insert" anything - it creates a new People / Person with related information. So change it's name to GetDetail, and have it accept a pointer to a Person to fill in:
C
typedef struct Person
{
   int id;
   char name[20];
   int age;
} Person;

Person people[100];

Person* GetDetail(Person* p)
{
    printf("\nInsert name of person: ");
    scanf("%s", p->name);
    printf("Insert age of person: ");
    scanf("%d", &(p->age));

    return p;
}
int main()
{
    Person *p = GetDetail(&(people[0]));
    printf("%s:%d\n", p->name, p->age);
    return 0;
}
Try that, and see what happens!
 
Share this answer
 
You need to visit some C++ pointer tutorial to understand this complex topic. It is only a basic tutorials so search for furthers.

In your code you have the fix
C++
struct People array[100];
which in an array of structs and the code is accessing elements it via the insert function. The name "insert" is wrong because the code only fill the members of the elements. Better would be setPerson().

Use the debugger and view the array in the memory pane to see the details.

PS: I would use class design and create a class Person with these functions
 
Share this answer
 
A few simple corrections are required to your code:
C++
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct People
{
   int id;
   char name[20];
   int age;
};

void insert(struct People *array, int i)    // ***Note 1
{
    //&array[i]->id = &array[i]->id + 1; //im trying to increment id for every person
    printf("\nInsert name of %d. person: ", i);
    scanf("%s", array[i].name);             // ***Note 2
    printf("Insert age of %d. person: ", i);
    scanf("%d", &array[i].age);             // ***Note 3
}

int main()
{
    int i = 0, add;
    struct People array[100];

    printf("\nType 1 to add a new person ");
    scanf("%d", &add);
    while(add==1)
    {
        insert(array, i);    // ***Note 4
        i++;
        printf("\nType 1 to add a new person ");
        scanf("%d", &add);
    }
	for (add = 0; add < i; add++)
	{
		printf("%s is %d years old\n", array[add].name, array[add].age);
	}
}

Notes:
1. Parameter 1 is a pointer to the first element of an array of People structs, so it does not require the square brackets.
2. The name variable is an array of characters so it does not need the adressof operator (&).
3. However, the age variable is a single integer so it does need the adressof operator.
4. The insert function requires the address of the array, so it only needs to pass the name, as that is a pointer to the first element.
 
Share this answer
 
v3
Comments
Member 14785794 28-Dec-21 9:48am    
Your explanation is perfectly elaborated, thank you!

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