-4

I don't understant how I can call this function correctly in main.
I tried calling like this: X *p1=malloc(sizeof(X));read(&p1);

struct x
{   int c;
    char n[250];
    char u[150];
};
typedef struct x X;

void myread(X *p);

void myread(X *p)
{
    scanf("%d",&p->c);
    fgets(p->n,sizeof(p->n),stdin);
    fgets(p->u,sizeof(p->u),stdin);
}
void main(){
    X *p1;
    p1=malloc(sizeof(X));
    myread(p1);
}
user6131790
  • 39
  • 1
  • 1
  • 5
  • 1
    Take the [tour], read [Ask] and [MCVE]. – jwdonahue Feb 23 '18 at 06:30
  • 1
    Study pointers and the & operator. – MFisherKDX Feb 23 '18 at 06:31
  • 1
    `gets` is obsolete, dangerous and should never be used. Use `fgets` instead. Take several weeks to read a good C programming book. – Basile Starynkevitch Feb 23 '18 at 06:33
  • @user202729 I studies structures by my own and I try to get some data and put it on in a file.txt. And this is the part where I want to add the information. – user6131790 Feb 23 '18 at 06:35
  • 2
    You could improve your question by adding information of why you (correctly) think that your way of calling is wrong. E.g. you probably got compiler warnings, show them. – Yunnosch Feb 23 '18 at 06:50
  • You edited your question, incorportating the answers. This makes the question weird and harder to understand. You did not take the opportunity to improve it by adding information. Please note that the proposal to add e.g. the compiler warnings, got some explicit approval by other readers. – Yunnosch Feb 23 '18 at 12:06

2 Answers2

3

The function as declared in your question (void read(X *p)) needs a pointer to X as parameter.
Your declaration of (X *p1) ensures that it is a pointer to X.
Your initialisation (p1=malloc(sizeof(X));) makes it a pointer to a suitably sized piece of useable memory.

That's it.

You then however use that appropriate pointer with the "take the address operator" &, which results in a pointer-to-pointer to X.

Calling the function with p1 as parameter would look like

read(p1);

I.e. just give the pointer to X as it is.

The function looks like it is supposed to fill the malloced space with values.
How that works and how it could be improved is not part of your question.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
2

Don't use gets. It is dangerous and obsolete. Use fgets instead. Replace gets(p->n) with fgets(p->n, sizeof(p->n), stdin); and so on.

Don't name your function read (since that is the name of a POSIX standard function). Replace it by another name, e.g. myread.

You probably want to do:

X *p1=malloc(sizeof(X));

in your main. Then, you need to check that malloc succeeded:

if (!p1) { perror("malloc p1"); exit(EXIT_FAILURE); }

Beware that malloc gives (on success) some uninitialized memory zone. You may want to clear it using memset(p1, 0, sizeof(p1)), or you could use p1 = calloc(1, sizeof(X)) instead.

At last you can pass it to your myread:

myread(p1);

Don't forget to call free(p1) (e.g. near the end of your main) to avoid a memory leak.

Learn to use valgrind, it catches many memory related bugs.

Of course you need to carefully read the documentation of every standard function that you use. For example, fgets(3) documents that you need to #include <stdio.h>, and that call to fgets can fail (and your code needs to check that, see also errno(3) & perror(3)...). Likewise, malloc(3) wants #include <stdlib.h> and should be checked. And scanf(3) can fail too, and needs to be checked.

You should compile with all warnings and debug info (gcc -Wall -Wextra -g with GCC), improve your code to get no warnings, and you should use the gdb debugger; you may want to use GCC sanitizers (e.g. instrumentation options like -fsanitize=address, -fsanitize=undefined and others),

Beware of undefined behavior (UB). It is really scary.

PS. I hope that you are using Linux with gcc, since it is a very developer friendly system. If not, adapt my answer to your operating system and compiler and debugging tools.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • Still have problems. implicit declaration of malloc, the c is reading, but n and u it' s not. I modified in the question the code. – user6131790 Feb 23 '18 at 09:00
  • Even your improved question don't check against failure of `malloc`, of `fgets`, of `scanf`. You should add the checks; for example when `fgets` fails your code might have UB (later) – Basile Starynkevitch Feb 23 '18 at 09:22
  • 1
    Your question is about how to call a function with pointer parameter. Please ask a separate question if you need help with using standard functions correctly. Please consider the answers in the light of the question they attempt to answer. Both (one is mine, but I mean both) do answer the question as asked. If you agree, consider choosing the one which helped more and accepting it. – Yunnosch Feb 23 '18 at 12:10