1

So I basically have a struct with a name, which has to be dinamically, and an ID. I think it can look like this.

typedef struct {
char *name;
unsigned int id;
} person;

Now I shall write a function with the given start:

person *readData();

Both the struct and the name have to be dinamically, I want to do this with the malloc function. For all persons there should also be an array, let's name it "people[1000]".

Here are is my try on the said function with the main function:

int count = 0;

person *readData() {
    int i, len;
    char puffer[1000];

    printf("Name: ");
    scanf_s("%999s", &puffer);

    len = strlen(puffer);
    people[count].name = (char *)malloc((len + 1)*sizeof(char));

    for (i = 0; i < len; i++)
        people[count].name[i] = puffer[i];

    people[count].name[len] = '\0';
}

void main(void)
{
    person *people[1000];
    readData();
    printf("\n%s\n", people[count].name);
}

Well, it doesn't seem to work that way. Visual Studio says in the function, that "people" has to be of type union or struct. Any quick input? It's just basic C since I am at the start of learning it.

EDIT: Full code:

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

typedef struct {
char *name;
unsigned int id;

} person;

person people[1000];

int count = 0;

person *readData() {
int i, len;
char puffer[1000];
printf("Name: ");
scanf_s("%999s", &puffer);
len = strlen(puffer);
people[count].name = (char *)malloc((len + 1)*sizeof(char));
for (i = 0; i < len; i++)
    people[count].name[i] = puffer[i];
people[count].name[len] = '\0';


}

void main(void){
readData();
printf("\n%s\n", people[count].name);

}
1
  • Your array of pointers (people) is local to the main() function, so it's impossible for readData() to know about it without you passing it (or making it global). You probably didn't mean person *readData() but void readData(person* people). Your code contains a lot of other problems too though. Commented Jan 9, 2014 at 11:52

2 Answers 2

2

people variable is undefined in the body of your readData function. That is why the complier fails with error.

  1. You can pass variable to the function person *readData(person *people). Do not forget to change readData(); to readData(people); in the main function.

  2. Do not mix pointer notation with array notation if you need just 1-dimensional array. Use person people[1000]; instead of person *people[1000];

Sign up to request clarification or add additional context in comments.

8 Comments

Thanks for your quick response. Since I got this function without any parameters and I got others >with< parameters I suppose I shall do it without. That's the main problem I currently have and that's why I wanted to make "person people[1000]" global.
Just declare your variable outside of main function body if you really want to do it.
Even with a parameter I still get the error in the struct function where I want to say "people[count].name". The error regarding count says that it has to be of type pointer-to-object. Do you have an idea regarding that as well?
Did you consider my 2nd suggestion?
Yeah, I applied that as well.
|
0

Your function readData doesn't know about your people array. If you rewrite it along these lines:

void readData(person** people) {
    your code
}

void main() {
   person* people[100];
   readData(people);
}

It should go better.

Please see zavgs answer for more things to improve.

2 Comments

Any way to do it w/o parameters?
@user3177280 Not so easily. But what is the problem with parameters?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.