Thread: PLease give a revision for pointerarrays!

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    34

    Unhappy PLease give a revision for pointerarrays!

    Hello again!

    I make a little Segfault while taking acces to to a
    pointer array ... . This is my declaration:

    Code:
    struct data {
    char *pointerarray[10][10][10];
    }mystruct;
    char *tmpchar;
    If I read data from stdin, like this:

    Code:
    scanf("%s[^\n]",&tmpchar);
    and try to put it into the pointerarray, like this:

    Code:
    strcpy(mystruct.pointerarray[0][0][0], tmpchar);
    I get a Segmentation Fault! Why ?

    demonus

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    34

    APPENDIX

    the variable HAS to be a arraypointer because I need a 3 dimensional variable where I can put MORE THAN ONE character per address into it:

    arraypointer[0][0][0]= "Hello"
    arraypointer[1][1][1] = "my friends"

    And they must be strings

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    34

    RE

    declaring tmpchar as an array brings the same error:
    Segfault !

    >> arraypointer[0][0][0]= "Hello"
    >> arraypointer[1][1][1] = "my friends"
    >So what is in arraypointer[0][0][1] or arraypointer[0][1][0] or >arraypointer[1][0][0] ?

    Do you know K.I.S.S. ? Keep It Short and Simple!

    In fact the user enter ALL variables in a for(); - loop ... .
    So ALL spaces will be used:

    Code:
    printf("Enter field 1: "); scanf("%d",aninteger);
    This declares arraypointer[aninteger][0][0] !
    Do you know what I mean ?

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    34

    RE

    Code:
    define MAX 10
    struct data {
    char *pointerarray[MAX][MAX];
    }mystruct;
    char *tmpchar, *tmpchar2;
    int i,j;
    main()
    {
    printf("Enter first field: ");
    scanf("%d",i);
    for(j=0;j<=MAX;j++)
    {
    printf("Enter second field [%d]: ",j);
    scanf("%s[^\n]", &tmpchar);
    // Put malloc() here; I don't know the right syntax yet...
    strcpy(mystruct.pointerarray[i][j][0], tmpchar);
    }
    printf("ready!\n");
    
    ...
    }
    Do you mean this ?

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    124
    demonus,
    >This declares arraypointer[aninteger][0][0] !
    You can't have an array which can have both integers and characters. Either only characters either only integers.

    >Do you know K.I.S.S. ? Keep It Short and Simple!
    ?? Is it short and simple to have an array of array of array of pointers??

    Also, i really doubt if you need to make something like *pointerarray[10][10][10]

    If you say what your problem asks you to do here, someone could help you find out if you really need that.
    Loading.....
    ( Trying to be a good C Programmer )

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826

    Re: RE

    Originally posted by demonus
    Do you know K.I.S.S. ? Keep It Short and Simple!
    Actually it's "Keep It Simple, Stupid".

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Give me some opinions on setting up a server
    By Shadow in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 04-19-2004, 10:38 AM
  2. Can you give me your tip plz :)
    By dionys in forum C Programming
    Replies: 6
    Last Post: 04-11-2004, 11:14 PM
  3. Replies: 1
    Last Post: 10-01-2001, 10:39 AM
  4. How To Give A Font Colour ?
    By Unregistered in forum Windows Programming
    Replies: 1
    Last Post: 09-14-2001, 01:22 PM
  5. Just to give you an idea of what we're going through...
    By rick barclay in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 09-13-2001, 02:09 PM