0

I have initialized a Structure through a Pointer and I have tried to get values using Pointer too. I am getting Segmentation Fault. Any Clue on it?

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


int main(void){
    struct MemData{
        char* FileName;
        int LastByteLength;
        int ReadPointer;
        int WritePointer;
        char Data[ 512000];//MEMORY BLOCK SIZE: 500 KB
    };
    struct MemData* M;
    M->FileName=(char*)"xaa";
    M->LastByteLength=0;
    M->ReadPointer=-1;
    M->WritePointer=-1;
    printf("\n%s", M->FileName);
    printf("\n%d", M->LastByteLength);
    printf("\n%d", M->ReadPointer);
    printf("\n%d", M->WritePointer);

}
protik
  • 75
  • 5
  • 3
    `struct MemData* M;` declares a pointer, but you never initialized it. `M` points nowhere. – Jabberwocky May 20 '19 at 07:05
  • 1
    Maybe you want : `struct MemData M;` (without the `*`) and change all `M->` to `M.`. Read the chapter dealing with structs and the one dealing with pointers in your C text book. – Jabberwocky May 20 '19 at 07:07
  • You forgot to initialize your pointer `M`, so of course you're getting a segmentation fault. What did you expect it to point to? It's undefined behavior. – Tom Karzes May 20 '19 at 07:12
  • Not related to your question, 512kib = 512*1024, which is probably what you need. – Lundin May 20 '19 at 08:03

1 Answers1

1

You need to allocate M.

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


int main(void){
  struct MemData{
     char* FileName;
     int LastByteLength;
     int ReadPointer;
     int WritePointer;
     char Data[ 512000];//MEMORY BLOCK SIZE: 500 KB
  };
  struct MemData* M;
  M = malloc(sizeof(*M));
  M->FileName="xaa";
  M->LastByteLength=0;
  M->ReadPointer=-1;
  M->WritePointer=-1;
  printf("\n%s", M->FileName);
  printf("\n%d", M->LastByteLength);
  printf("\n%d", M->ReadPointer);
  printf("\n%d", M->WritePointer);
  free(M);
}

Try the above code, it should works.

As Broman suggested in comments I edited the answer removing the unnecessary "(char*)" cast as string literal already has the type char*

ieio
  • 173
  • 9
  • 2
    [Don't cast malloc](https://stackoverflow.com/a/605858/6699433) – klutt May 20 '19 at 07:31
  • Also, instead of `sizeof(struct MemData)`, use `sizeof (*M)` – klutt May 20 '19 at 07:36
  • 1
    Reason: What happens if you want to rename `MemData` or change the code to use another struct? Then you need to change the code on several places. – klutt May 20 '19 at 07:38
  • This allocates memory and points `M` to that memory. It doesn't allocate `M` (which is already allocated automatically) – M.M May 20 '19 at 07:44
  • Broman thanks for the good hints, I edited the code accordingly. – ieio May 20 '19 at 07:45
  • You should also remove the `(char*)` cast. It's completely unnecessary. A string literal already has the type `char*`. (Well, not really but it's close enough) – klutt May 20 '19 at 07:49
  • I know OP wrote like that, but why not fix what can be fixed? You could also improve your answer by a short note about why you removed the casts. – klutt May 20 '19 at 07:50