0

I'm looking for a simple solution to this. I only have one element and could easily turn it into a simple array of long integers but in the future I will have a struct of random data types so instead of me declaring a bunch of separate arrays of random types, I want to pack the data into a struct.

In this code the problem lies with the calling of load() but I don't know how to solve it. When I use the &, in front of the struct variable, the compiler reports warning: passing argument 1 of 'load' from incompatible pointer type.

The output I expect instead of errors or a segmentation fault is:

0= 1
1= 11

What am I doing wrong?

and here's the code:

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

typedef struct{
    long n;
}a;

void load(a** x){
    x[0]->n=1;
    x[1]->n=11;
}

int main(void){
    a** b=malloc(200);
    b[0]->n=2;
    b[1]->n=2;
    load(&b); //trouble starts here
    printf("0= %ld\n",b[0]->n);
    printf("1= %ld\n",b[1]->n);
    free(b);
    return 0;
}
1

3 Answers 3

2

You don't need pointer to pointers. Just use

a* b=malloc(ELEMENTS * sizeof (a)); // essentially array with nr of ELEMENTS of type a

The function

void load(a* x){
    x[0].n=1; // 0th structure object
    x[1].n=11; // 1th structure object .. you can access till ELEMENT-th index
}

You can call it like

load(b);  // you can directly pass the pointer
Sign up to request clarification or add additional context in comments.

2 Comments

Also, note that malloc returns void pointer . So, another better practice can be to typecast with the appropriate pointer.
@Karthik Balaguru: not in C, in C you don't cast result of malloc
0

Anytime you run malloc, you need to check that it has not returned a NULL pointer (ptr == 0). If you try and access a NULL pointer, it can throw a segmentation fault.

One way to do this would be...

a** b=malloc(200);
if(b != 0)
{
   //The rest of the code.
}

2 Comments

That is easy to catch but I just found out my segmentation fault happens when b[0]->n=2; is executed if I change load(&b) to load(b)
If b is a pointer to a pointer of a (what IS a), then you're getting some funky pointer arithmetic off of that, which is one more way to segfault. @Giorgi is absolutely right in his answer.
0

Thanks for the help, but I couldn't accept one answer because everyone solved portions of the problem.

Here's what I came up with:

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

typedef struct{
    long n;
}a;

void load(a* x){
    x[0].n=1;
    x[1].n=11;
}

int main(void){
    a* b=calloc(1,sizeof(a)*100);
    if (!b){printf("memory error\n");return 1;}
    b[0].n=2;
    b[1].n=2;
    load(b);
    printf("0= %ld\n",b[0].n);
    printf("1= %ld\n",b[1].n);
    free(b);
    return 0;
}

2 Comments

problems solved? though strange why put long inside a struct like that
yes. I was doing this as an example but later I will use more than just a long inside a struct.

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.