1

Very confused on why I'm getting a "Segmentation Fault (core dumped)".

const int bufSize = 32;
char* splitStrings[bufSize];
splitStrings[0][0] = 'a';
splitStrings[0][1] = '\0';
printf("testchar: %c",splitStrings[0][0]);
printf("teststr: %s",splitStrings[0]);

Thanks for any help

Coder77
  • 2,203
  • 5
  • 20
  • 28

4 Answers4

3

You missed a layer of declaration. You have a table of pointer on char splitStrings. Then you would need to book some memory space and point to it using your pointer. Here is the example for the first pointer:

splitStrings[0] = malloc(2*sizeof(char));
splitStrings[0][0] = 'a';
splitStrings[0][1] = '\0';
printf("testchar: %c",splitStrings[0][0]);
printf("teststr: %s",splitStrings[0]);

Read how malloc is working and especially how to de-allocate memory with free once your done to avoid memory leak.

Puck
  • 2,080
  • 4
  • 19
  • 30
  • Because by definition `sizeof(char)` will always == 1, your statement can be simplified to `...malloc(2);` – ryyker Nov 21 '18 at 13:29
2

I think you want to define a 2-D char array.

To do this make these steps:

First

Define a char** array of pointers.

char **splitStrings[bufSize];

Second

Allocate memory for row:

splitStrings = malloc(bufSize*sizeof(char*));

Third

For each row allocate memory for its columns.

for (int i=0;i<bufSize;i++){
   splitStrings[i]=malloc(bufSize*sizeof(char*));
}

Code

#include <stdio.h>///for printf()
#include <stdlib.h>///for malloc()

int main(void){

    const int bufSize = 32;
    char **splitStrings;

    splitStrings = malloc(bufSize*sizeof(char*));

    for (int i=0;i<bufSize;i++){
        splitStrings[i]=malloc(bufSize*sizeof(char*));
    }

    splitStrings[0][0] = 'a';
    splitStrings[0][1] = '\0';

    printf("testchar: %c\n", splitStrings[0][0]);
    printf("teststr: %s\n", splitStrings[0]);

    free(splitStrings);

    return 0;
}

Further Notes

  • For more information about 2-D dynamic array see
  • By using malloc() function in C never cast the result of it see
  • After allocating dynamic memory in Heap never forget to release it by using free().

  • I suggest you read more about how to allocate dynamic memory in C with malloc() function.

Last Edition on code

Change malloc(bufSize*sizeof(char)); to malloc(bufSize*sizeof(char*)); this is true way of allocating memory

Because in first case calling free() on splitStrings pointer cause memory error. Why?

EsmaeelE
  • 2,331
  • 6
  • 22
  • 31
  • 2
    I believe OP is still a begginer in terms of C, and given the question's content, I would assume he will have a lot of trouble understanding this answer without any explanation or comment. – babu646 Nov 21 '18 at 12:49
1
char* splitStrings[bufSize]; //is expecting address, if it is derefencing directely will through segmentation fault.

we have two options one is dynamic memory allocation otherwise declare like below.

#include<stdio.h>
void main()
{
    const int bufSize = 32;
    char splitStrings[1][bufSize];
    splitStrings[0][0] = 'a';
    splitStrings[0][1] = '\0';
    printf("testchar: %c",splitStrings[0][0]);
    printf("teststr: %s",splitStrings[0]);
}
1

Others have answered how to fix the error, so I won't go over that. Instead, I will answer the question of why you are getting a segmentation fault.

So, let's step through your code and see what's happening!

const int buffsize = 32;
char* splitStrings[buffsize];

Alright! So far, you have declared a const int of size 32 (good use of const btw!), and you have created a pointer to an array of characters of size 32!

So let's look at the next line!

splitStrings[0][0] = 'a';

So now, you are trying to look at the first item in the array that splitStrings points to, and then looking at the first item in that array. And this is where you get your segmentation fault!

In C, a segmentation fault occurs when something tries to access memory that it is not allowed to access. In this case, it's alright for you to access splitStrings[0], but not splitStrings[0][0]. This is because you currently don't have an array at splitStrings[0] - instead, you just have an unassigned pointer to a character.

So, when the compiler tries to work through that, it says "Okay, let's look at the first item in splitStrings! Alright, that's a pointer to a character! Now, let's look at the first item under that character! It's - wait, hold on. I haven't assigned that yet, it isn't allowed to have its own array yet! Segmentation fault!"

To fix this, you will need to create a 2D array (that's an array of arrays) and allocate that memory. EsmaeelE has given good instructions on how to do that in their answer here. I also highly recommend the TutorialsPoint segments on multi-dimensional arrays and arrays of pointers. The site is an excellent resource for anyone trying to learn the C programming language!

I hope that helped you understand the segmentation fault better! Feel free to ask for any clarifications!

MrSpudtastic
  • 205
  • 2
  • 18