I send a pointer to an array of char* to a function (args). In that function I set the value to the first two postions in the array to malloc’d strings. When I return to the orignal calling function where the array itself was malloc’d, the last position of the array gives me a “Cannot access memory at address 0x0”. Am I doing somthing wrong in malloc/realloc/ or storing the values?
Calling Function:
int bufspace = 0; /* bytes in table */
...
args = emalloc(BUFSIZ); /* initialize array */
bufspace = BUFSIZ; //size=8192
spots = BUFSIZ / sizeof(char *);
while (*cp != '\0') //While not at the end
cp = read_segment(cp, &len, &indollarsign, &argnum, start, &args, &prev_char_escape);
start = cp;
len = 0;
if (argnum + 1 >= spots) {
args = erealloc(args, bufspace + BUFSIZ);
bufspace += BUFSIZ;
spots += (BUFSIZ / sizeof(char *));
}
}
Called Function (read_segment) – First called to store at position 0 then at position 1:
*args[*argnum] = newstr(start, *len); //Generate the string through malloc
At this line, I have strings in these positions: *args[0] and *args[1]
But as soon as I return to the calling function
args[0] has a string but args[1] shows a “Cannot access memory at address 0x0”
*args[*argnum]dereferences the pointer atargs[*argnum]. I think you meant