d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Problem With C Seg Fault Issue
Add Reply New Topic New Poll
Member
Posts: 24,101
Joined: Nov 8 2007
Gold: 5,561.70
Apr 27 2013 09:58am
Ok so I have an allocated array of strings, lets call it **temp_array

My structures defined below, pretty much I'm trying to store the temp_array[0] into first element, and then store everything else in **temp_array into **new_array

I'm getting a segfault when trying to store anything into **new_array, and can't figure out why.
Code
typedef struct folder{
        char *first_element;
        char ** new_array;

}Folder;
int i = 1, j = 0;
Folder *new;

new->first_element = temp_array[0]; /* this works fine*/

/* no idea how to point to the temp_array from new_array without segfaulting? */

while(temp_array[i] != NULL){
     new->new_array[ j ] = temp_array[ i ]; /* this code isn't working, seg faults */
     j++;
     i++;
}


This post was edited by lopelurag on Apr 27 2013 09:58am
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Apr 27 2013 11:52am
because you havent allocated any memory to the new array.

pointers point to a random memory address until it is given a address to point to, and with that being said you are trying to write data to somewhere random in memory outside your memory scope and thus segfaulting.

with that being said go search up, malloc, calloc, alloc, and free.

This post was edited by AbDuCt on Apr 27 2013 11:53am
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll