I've been working on this problem for quite some time now, can't seem to find an answer anywhere. The problem is fairly simple, I was given a sample program and asked to fix the error (This is a homework problem for University).
Here is the program in C:
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
int a;
char *b;
} Cell;
void AllocateCell(Cell * q)
{
q =(Cell*) malloc (sizeof(Cell));
q->b = (char*)malloc(sizeof(char));
}
int main (void)
{
Cell * c;
AllocateCell(c);
c->a = 1; // Produces Seg Fault
free(c);
return 0;
}
The Segmentation fault is produced by the line:
c->a = 1;
Clearly the seg fault comes from some part of the memory allocation, but I'm not quite sure from where.