Thread: Confusion in pointers in C

  1. #1
    Registered User
    Join Date
    Apr 2023
    Posts
    4

    Confusion in pointers in C

    Hi all,
    Here is another question for me that i could not find any answer. Try to create an dynamic array in C just using vscode in Ubuntu. Try to describe my issue in code sample below. When i run the code output is: [hello0][(null)]
    When i use line 10 in code sample taking this error message "Exception has occurred Segmentation fault. Could you please tell me what i am doing wrong. I would be much appreciated.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    int foo(char ***BufferArray,char *item,int idx);
    int main(){
        char **ptr=malloc(sizeof(char*)*3);
        char a[]="hello0";
        char b[]="hello1";
        foo(&ptr,a,0);
        //foo(&ptr,b,1);//Exception has occurred. Segmentation fault  - line 10
        printf("[%s]",*(ptr+0));
        printf("[%s]",*(ptr+1));
        getchar();
        return(0);
    }
    
    int foo(char ***BufferArray,char *item,int idx){
        int n;
        n=1;
        *(*(BufferArray+idx))=malloc(sizeof(char)*strlen(item));
        strcpy(*(*(BufferArray+idx)),item);
        return(0);
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    int foo(char ***BufferArray,char *item,int idx);
    int main(){
        char **ptr=malloc(sizeof(char*)*3);
        char a[]="hello0";
        char b[]="hello1";
    
        foo(&ptr,a,0);
        foo(&ptr,b,1);
        printf("[%s]\n",*(ptr+0));
        printf("[%s]\n",*(ptr+1));
    
        free(ptr[0]);
        free(ptr[1]);
        free(ptr);
        return(0);
    }
     
    int foo(char ***BufferArray,char *item,int idx){
        *((*BufferArray)+idx)=malloc(sizeof(char)*(strlen(item)+1));
        strcpy(*((*BufferArray)+idx),item);
        return(0);
    }
    Two things.
    1. You forgot to count the \0 when allocating space for the string.
    2. You need to dereference BufferArray and then index it.

    BufferArray points to ptr in main, and you want to effectively do ptr[idx] = malloc...

    Writing it like this saves a bunch of stars and brackets.
    Code:
        (*BufferArray)[idx]=malloc(sizeof(char)*(strlen(item)+1));
        strcpy((*BufferArray)[idx],item);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Confusion with pointers!
    By ranadas in forum C Programming
    Replies: 1
    Last Post: 11-28-2017, 03:27 AM
  2. Confusion with pointers
    By Stormboy in forum C Programming
    Replies: 8
    Last Post: 08-31-2013, 08:49 AM
  3. Confusion With Pointers
    By ObjectWithBrain in forum C Programming
    Replies: 28
    Last Post: 07-14-2011, 11:44 AM
  4. Confusion using pointers
    By kluxy in forum C Programming
    Replies: 10
    Last Post: 03-27-2010, 12:07 AM
  5. Confusion about pointers
    By rishiputra in forum C Programming
    Replies: 1
    Last Post: 05-01-2003, 04:39 PM

Tags for this Thread