How to correctly assign a pointer to a structure variable in this case?

11,386

Solution 1

struct fraction *fp;
struct fraction f1,f2;
fp=&f1;

is indeed similar to

int x=10;
int *y;
y=&x;

However, did you try to use the latter y=&x; outside any functions like what you did with fp=&f1;? That would not compile because for both examples it is not legal for the assignment to be put there.

The solution is simple: put fp=&f1; inside a function, like main.

Solution 2

As @Yu Hao suggested either you can put fp=&f1; inside a function, like main Or

You can change your code to :

struct fraction f1,f2;
struct fraction *fp = & f1;
struct fraction num[3];
Share:
11,386
Manish Giri
Author by

Manish Giri

I love fiddling with code. I like to solve problems, build interesting things, and I'm constantly amazed by the power you get with the knowledge of coding. Currently working as an SDE-2 at Dell Technologies. Graduated with a Masters in Computer Science in December 2020 from the University of Cincinnati. Okay, seeing as I'm generally not very good with "About Me's", gonna stop here. Feel free to hit me up through any of the links on the right. Oh, and there's something I very strongly believe in - “Everybody in this country should learn to program a computer, because it teaches you how to think” - Steve Jobs

Updated on June 04, 2022

Comments

  • Manish Giri
    Manish Giri almost 2 years

    Can someone please point out what's the error in this assignment of pointer to a structure?

    Brief background- I have a structure called fraction, I have two variables of type fraction (f1 &f2). I also have an array of structures called num. It all worked well so far until I tried to create a pointer of type struct fraction and assign it f1's address.

    #include <stdio.h>
    
    struct fraction
    {
        int nume;
        int deno;
    };
    
    struct fraction *fp;
    struct fraction f1,f2;
    struct fraction num[3];
    fp=&f1;
    
    int main()
    {
        f1.nume=22;
        f1.deno=7;
        int i,x=5,y=6;
        f2=f1;
    
        printf("\n Fraction 1 numerator is: %d\n", f1.nume);
        printf("\n Fraction 1 denominator is: %d\n", f1.deno);
    
        printf("\n Fraction 2 numerator is: %d\n", f2.nume);
        printf("\n Fraction 2 denominator is: %d\n", f2.deno);
    
        //initialize array of structs
        for(i=0;i<3;i++)
        {
            num[i].nume=x;
            num[i].deno=y;
            ++x;
            ++y;
        }
    
        //print out array of structs
        for(i=0;i<3;i++)
        {
            printf("\n Numerator of fraction num %d : %d", i, num[i].nume);
            printf("\n Denominator of fraction num %d : %d\n", i, num[i].deno);
    
        }
    
        //try to use pointer to access field
        printf("\n Numerator of fraction f1 : %d\n", fp->nume);
        printf("\n");
    }
    

    I was thinking pointers could be assigned to variables of a structure type following the normal rules of pointer assignment with int, float etc. For instance- you could do:

    int x=10;
    int *y;
    y=&x;
    

    This points the pointer y to the address of variable x.

    Then, following the same logic, why does the code above not work? I tried to verify my method online, and from this eskimo.com article, it says pretty much the same thing. Here's the error I get:

    struct1.c:21:1: warning: type specifier missing, defaults to 'int'
          [-Wimplicit-int]
    fp=&f1;
    ^~
    struct1.c:21:1: error: redefinition of 'fp' with a different type: 'int' vs
          'struct fraction *'
    struct1.c:17:18: note: previous definition is here
    struct fraction *fp;
                 ^
    

    Many thanks for the help!