copying an array of pointers into an multidimensional array

Posted on 2005-04-29
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?
Question by:v_wall78
    1 Comment
    LVL 45

    Accepted Solution

    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 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

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    C programming help for beginner sort routine 5 250
    negation in C function 14 127
    Finding a good hash function 4 109
    Grammars for C C++ and java 1 99
    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 opening and writing to files in the C programming language.
    Video by: Grant
    The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

    754 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now