[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

copying an array of pointers into an multidimensional array

Posted on 2005-04-29
1
Medium Priority
?
224 Views
Last Modified: 2010-04-15
I would like to know what I am doing wrong. I am keeping a history of commands and want to save argbuf each time I have used it into another array.
My code is like this:

char *argbuf[ARG_MAX+1];  
char **history[ARG_MAX+1];
   
if (his_count < HIS_MAX) {
    strcpy(*history[his_count], *argbuf);          // Add the command to history
    his_count++; }
else {                                           // Update history
    i = 0;
    while (i < (HIS_MAX-1)) {
        strcpy(*history[i], *history[i+1]);
        i++; }
     strcpy(*history[i], *argbuf); }

I get a segmentation fault, what am I doing wrong?
Thanks
0
Comment
Question by:v_wall78
1 Comment
 
LVL 45

Accepted Solution

by:
sunnycoder earned 2000 total points
ID: 13899276
char ** , char * [], char [] [] are three very different data types which canNOT be used interchangably.


char  ** a;
a = (char **) malloc ( sizeof(char *) *10 );

for ( i=0; i <10 ; i++ )
     a[i] = (char *) malloc ( sizeof(char ) *10 );

to get to [i][j], you need to access the ith * in a ... and in the array pointed to by a[i], you need to refer to jth element

consider the overall organization

a =    ptr0           ptr1           ptr2      .....                ptr9
           |                 |                |                                 |
           \/               \/               \/                                \/
        int[10]         int[10]       int[10]                         int[10]

a is the beginning address of a
a + i is the address of the ith element
*(a+i) is the address of array of 10 ints
*(a+i) + j is the address of jth element in this array
*(*(a+i) + j ) is the value of the jth element of this array

The above representation is fundamentally different from

a[10][10]

a is the address of beginning of array
a+i is the address of ith element... what this element is depends on value of i (read about row major and column major ordering?)
*(a+i) is the value at that location

*(a+i)+j is that value+j

*(*(a+i) + j )
is trying to derefernce that value and is likely to crash ...


Also se Kdo's post here for a simple explanation
http://www.experts-exchange.com/Programming/Programming_Languages/C/Q_20823846.html
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question