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

struct Fraction { 
    int num;
    int denom;
};

struct PolyTerm {
    int expo;
    struct Fraction coeff;
};

struct PolyNode {
    struct PolyTerm* dataPtr;
    struct PolyNode* next;
};

typedef struct Fraction* FractionAddr;
typedef struct PolyNode* PolyNodeAdr;
typedef struct PolyNode* PolyList;

int main() {
    int exponet;
    PolyNodeAdr polyNode = 0;

    printf("\n\tPlease Enter expoent: ");
    scanf("%d", &exponet);

    polyNode->dataPtr->expo = exponet;

    //printf("\n%d\n",polyNode->dataPtr->expo);

    return;
}

on the above code, I am trying to store the exponet into the expo in the struct of polynode

but I tried many ways, but errors keep appearing

isn't expo is an int? why I can't store the exponet (int) into it?

I checked a few ways, when I just put struct PolyTerm dataPtr;in the struct of polyNode

and polyNode->dataPtr.expo = exponet; in the main, it would work

I think because the dataPtr is a pointerstruct PolyTerm* dataPtr;

but I have no idea to fix it

can anyone explain to me why I can't do that and what is the solution for it?

Rex Rau
  • 103
  • 1
  • 1
  • 5

3 Answers3

0

You are Dereferencing a NULL pointer.

polyNode == NULL

dataPtr == anything.

so polyNode->dataPtr->expo is actually (NULL)->dataPtr->expo. it doesnt have meaning. there is segmentation fault because you are trying to access a restricted memory. thats why windows pops that message.

EDIT:thanks to @Nik for pointing out the errors in my answer.

Koushik Shetty
  • 2,146
  • 4
  • 20
  • 31
  • First of, `dataPtr` isn't `NULL`: it's *nothing at all*. `polyNode->dataPtr->expo` is actually `(NULL)->dataPtr->expo` - dereferencing a `NULL` pointer is bad. Secondly, `polyNod` isn't a struct - it's a *pointer* to a struct, and initializing a pointer to `0` is perfectly valid and correct. – Nik Bougalis Apr 02 '13 at 01:37
  • @NikBougalis thanks for pointing them out. corrected my answer. hope it dint mislead the OP. – Koushik Shetty Apr 02 '13 at 05:00
0

No memory was allocated for PolyNodeAdr polyNode

You have to add this after your declaration of polyNode for polyNode->dataPtr->expo = exponet; to work

polyNode = malloc( sizeof( struct PolyNode )) ;
polyNode->dataPtr = malloc( sizeof( struct PolyTerm )) ;

Note the usage of struct PolyNode not PolyNodeAdr since you changed PolyNodeAdr to a pointer with typedef.

Also you shouldn't typedef a pointer, since you lose the information that the name is a pointer.

For example:

typedef struct PolyNode* PolyNodeAdr;

Should be:

typedef struct PolyNode PolyNodeAdr;

So later you declare:

PolyNodeAdr * polyNode;
0

You have to allocate memory for all pointers that you gonna dereference. And free the memory after you are done with it.

int main() {
    int exponet;
    PolyNodeAdr polyNode = (PolyNodeAdr)malloc(sizeof(PolyNode));
    polyNode->dataPtr = (PolyTerm*)malloc(sizeof(PolyTerm));

    printf("\n\tPlease Enter expoent: ");
    scanf("%d", &exponet);

    polyNode->dataPtr->expo = exponet;

    //printf("\n%d\n",polyNode->dataPtr->expo);

    free(polyNode->dataPtr);
    free(polyNode);
    return 0;
}
Kupto
  • 2,802
  • 2
  • 13
  • 16
  • oh, actually i have the allocate memory for the node, but not for the dataPtr, that why it was not working, I thought polyNodePtr is the only pointer, I didn't think i need to allocate the pointer inside it. ok thank you! – Rex Rau Apr 02 '13 at 01:42
  • Please don't cast the result of `malloc` in C. It's not necessary and some even [argue that it's bad practice](http://stackoverflow.com/questions/1565496/specifically-whats-dangerous-about-casting-the-result-of-malloc) to do it. – Nik Bougalis Apr 02 '13 at 15:49