0
typedef struct line {
 int a;
 int b;
} line;
int main() {
 line *v;
 int c, d, j;
 char a;
 int i;
 scanf("%d", &n)
 for (i = 0; i < n; i++) {
 scanf("%c", &a);
 v = (line*) malloc(n * sizeof(line));
 if (a == '+') {
 scanf("%d %d", &v[j]->a, &v[j]->b);
 }
 }

I want to make an array that holds a struct info, and then use that info in my function . But I am getting some errors and I don't know if I used well the pointer.

I have tried with v[j].a but it didn't work. (I used this because I am more familiar from linked-list.)

chux
157k17 gold badges160 silver badges310 bronze badges
asked Apr 19, 2018 at 22:21
10
  • 1
    Bobo, Code is missing declaration of n. Suggest posting compilable code. Commented Apr 19, 2018 at 22:29
  • 1
    Rather than just say, "I am getting some errors", post the exact text of the error. That helps all of us. "it didn't work." is vague. Post how you determined or the evidence of how it "did not work". Commented Apr 19, 2018 at 22:30
  • 1
    Common error: scanf("%c", &a); --> scanf(" %c", &a); (Add space). Commented Apr 19, 2018 at 22:32
  • @P.Antoniadis , ty for pointing me the mistake at j , but i still get an error invalid type argument of ‘->’ (have ‘line {aka struct line}’) Commented Apr 19, 2018 at 22:33
  • regarding: scanf("%d", &n) This statement is missing the trailing ; The posted code does not compile! amongst other things it is missing the #include statement for stdio.h Commented Apr 21, 2018 at 17:24

2 Answers 2

1

Use &(v[j].a) instead, which is equal to &((v+j)->a). The name of an array is a pointer to the first element. So v[j] is an element of type struct line. In order to get one of its fields you use .a.

Also, check your code for some other errors because some variebles are not initialized.

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

2 Comments

@P.Antoniadis &(v[j].a) == &v[j].a. In this program OP allocates the array of the structs not the array to the structs.
@PeterJ_01 can you explain this a little bit?
0

First of all you malloc a new memory on every iteration and the previous one becomes unavailable.

Abstracting from the logic of the program

typedef struct line {
 int a;
 int b;
} line;
int main(void) {
 line *v;
 int c, d, j;
 char a;
 int i,n;
 scanf("%d", &n);
 v = malloc(n * sizeof(line));
 if(v == NULL) return -1;
 for (i = 0; i < n; i++) {
 scanf("%c", &a);
 if (a == '+') {
 scanf("%d %d", &v[j].a, &v[j].b);
 }
 }
}
answered Apr 19, 2018 at 22:39

2 Comments

scanf("%c", &a); --> Hmmm. Likely a will get the value of '\n'.
@chux - as I wrote -> abstracting from the logic of the program. I an not here to teach C. My answer is to show how to malloc and access the fields of struct in the allocated table and their addresses

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.