0

So I have created a struct, in which one of the variable is a pointer to a dynamic array of chars. So I implemented it as a pointer to pointer. Then I used a separate function to initialize the struct:

#include<stdio.h>
#include<stdlib.h>
//create a struct
typedef struct{
    //use a double pointer
    char **dynamicArray; 
    int size;
    int topValue; 
}Stack; 

/*
    Inintializes the stack
    Dyanmic Array will have a size of 2
*/
void intializeStack(Stack *stack){
    stack->size = 2; 
    stack->topValue = 0;

    //create a dyanmic array of char and set the value in the struct to the address for the newly created array
    char *dyanmic; 
    dyanmic = malloc(2 * sizeof(char)); 
    stack->dynamicArray = &dyanmic; 
}
int main(){

    Stack stack; 
    intializeStack(&stack);

    printf("stack.size: %d\n", stack.size);
    printf("stack.topValue: %d\n", stack.topValue); 
    int i; 
    for (i = 0; i < stack.size; i++){
        *(stack.dynamicArray)[i] = 'r'; 
        printf("%d value of the dynamic array: %c\n", i, *(stack.dynamicArray)[i]);
    }

    printf("Check if the stack is empty: %s\n",isEmpty(&stack)?"true":"false");

    return 0; 
}

The array is initially set to a size of 0. The problem is when I try to access the second element in the array, i get a segmentation fault error.

Segmentation fault (core dumped)

Am I doing something wrong in the implementation?

11
  • 2
    Undefined behavior for using the address of an object after the end of the object's lifetime. Commented Sep 19, 2017 at 19:45
  • 1
    Why the pointer-to-pointer? From everything I can see, you want single-indirection; not double. Commented Sep 19, 2017 at 19:48
  • @WhozCraig Because I have to dynamically change it's size, so I could create a temp array that's bigger than then set the pointer to that. Commented Sep 19, 2017 at 19:49
  • 1
    @VineetPatel you can do that with a char* Commented Sep 19, 2017 at 19:52
  • 2
    The memory of what? You're retaining the address of a local automatic variable that ceases to exist as soon as your function returns. There is a big difference between &dyanmic and dyanmic. See it live. Commented Sep 19, 2017 at 19:58

2 Answers 2

1

The following construct is confusing and ultimately incorrect:

for (i = 0; i < stack.size; i++){
      *(stack.dynamicArray)[i] = 'r'; 
      printf("%d value of the dynamic array: %c\n", i, *(stack.dynamicArray)[i]);
}

You are actually referencing the FIRST level of ** by this construction. Try this instead:

for (i = 0; i < stack.size; i++){
    stack.dynamicArray[0][i] = 'r';
    printf("%d value of the dynamic array: %c\n", i, stack.dynamicArray[0][i]);
}
Sign up to request clarification or add additional context in comments.

Comments

0

Abstracting ffrom the sense of this :)

char **dyanmic; 
dyanmic = malloc(sizeof(char *));
*dyanmic = malloc(2 * sizeof(char)); 
stack->dynamicArray = dyanmic; 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.