Reputation: 19
I am creating a struct using malloc:
structurePointer = (struct Person*)malloc(sizeof(struct Person));
struct Person
is referring to the following type:
struct Person {
char name;
int age;
}
What I am currently doing to load the struct is:
strcpy(&(structurePointer -> name), names);
names is just a pointer to an array element that is someones name which I pass to the function containing the above code. Than to load the age:
structurePointer + 1 -> age = ages;
Although adding 1 feels wrong as adding 1 would be pointing to the start of the next 32 bits or 16 bits depending on the architecture? If this is the way to do it, I don't understand how the compiler knows the whereabouts of the starting address of the age variable of the struct by adding 1 as obviously the name variable is of type char so it will be of arbitrary size?
Thanks I need to create an array of pointers to the structs, so I assume each array element will have the start address of the name and than using this to print the struct out I can print the age out by probably adding 1?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define HOW_MANY 7
//pointer declaration required?
char *names[HOW_MANY]= {"Simon", "Suzie", "Alfred", "Chip", "John",
"Tim", "Harriet"};
//pointer as it is an array
int ages[HOW_MANY]= {22, 24, 106, 6, 18, 32, 24};
struct Person {
char name;
int age;
}; //struct-person
//elements in the array consists of the struct Person type
struct Person *people[7];
//passing the pointer to the struct Person array
static void insert(struct Person *people[], char *names, int ages)
{
struct Person *structurePointer;
structurePointer = (struct Person*)malloc(sizeof(30));
int incrementVar = 0;
strcpy(&(structurePointer-> name), names); //
people[incrementVar]= structurePointer;
structurePointer -> age = ages;
incrementVar++;
} //insert-method
int main(int argc, char **argv)
{
struct Person *people[7];
for (int i = 0; i <= HOW_MANY - 1; i++) {
//passing name as a pointer as it has
insert (people, names[i], ages[i]);
}
for (int i = 0; i <= HOW_MANY - 1; i++) {
printf("%d ", people[i]-> age);
printf("%s", &(people[i] -> name));
printf("\n");
}
return 0;
} //main
Upvotes: 0
Views: 501
Reputation: 16540
regarding:
structurePointer + 1 -> age = ages;
this is wrong.
What you really want to remember is that adding a value to a pointer increases the pointer by the number of bytes in the pointer type (in this case sizeof( struct Person ))
And remember that the ->
operator has a higher precedence than the +
operator, so the order of operations is not correct.
Suggest using:
structurePointer->age = ages;
Upvotes: 0
Reputation: 53006
Your very little code has a lot of issues,
A char
variable is not of string type. There is NO string type in c and you need to read what a c-string is.
You call a unexsistent function strpy()
You claim to be using an invalid syntax, because 1->
is going to be a compilation error for sure.
In my opinion, you need to study more. To access structure elements if you allocated space for it correctly you just need to use the ->
operator, for example
struct Person {
char name[100]; // Be careful with array bounds here
int age;
}
struct Person *person = malloc(sizeof *person);
if (person != NULL) {
strcpy(person->name, "your name");
person->age = 33;
}
Upvotes: 2