Thread: two dimensional int array access problems ;/

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    6

    two dimensional int array access problems ;/

    Hi again

    I have a constant int array which looks like that:
    Code:
    const int *summaries[] = {{3, 4, 5, 6, 0}, {5, 0}};
    i guess there is something wrong with it becasue when compiling i get such warnings:
    Code:
    quota_prepare.c:28: warning: braces around scalar initializer
    quota_prepare.c:28: warning: (near initialization for `summaries[0]')
    quota_prepare.c:28: warning: initialization makes pointer from integer without a cast
    quota_prepare.c:28: warning: excess elements in scalar initializer
    quota_prepare.c:28: warning: (near initialization for `summaries[0]')
    quota_prepare.c:28: warning: excess elements in scalar initializer
    quota_prepare.c:28: warning: (near initialization for `summaries[0]')
    quota_prepare.c:28: warning: excess elements in scalar initializer
    quota_prepare.c:28: warning: (near initialization for `summaries[0]')
    quota_prepare.c:28: warning: excess elements in scalar initializer
    quota_prepare.c:28: warning: (near initialization for `summaries[0]')
    quota_prepare.c:28: warning: braces around scalar initializer
    quota_prepare.c:28: warning: (near initialization for `summaries[1]')
    quota_prepare.c:28: warning: initialization makes pointer from integer without a cast
    quota_prepare.c:28: warning: excess elements in scalar initializer
    quota_prepare.c:28: warning: (near initialization for `summaries[1]')
    And when i try to access one of the fields i get segmentation fault.

    Could You please tell me how to correct this?

    Best regards

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You don't have a two dimensional array there. You have an array of pointers to integers.


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

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    it should be some thing like this

    Code:
    int arr[][10]={{3, 4, 5, 6, 0}, {5, 0}};
    and here is a sample code on how to work on 2D array
    Code:
    #include<stdio.h>
    
    int main()
    {
        int arr[][10]={{3, 4, 5, 6, 0}, {5, 0}};
        int i,j;
    
        for(i=0;i<2;i++)
        {
            for(j=0;j<5;j++)
                printf("%d\t",arr[i][j]);
            
            printf("\n");
        }
        getchar();   
    }
    /*myoutput
    3       4       5       6       0
    5       0       0       0       0
    */
    ssharish2005

  4. #4
    Registered User
    Join Date
    Jan 2006
    Posts
    6
    Hi

    Thank You very much for this one ;] of course it works just fine.
    But i have one more questions about multid. arrays. Is there any way of knowing what is the size of the first dimension? I need it for my while loop. I always put 0 on the end of the field to know where it ends but i don't know how many fields there are and after second pass i get segmentation fault. I can put there {0} and do
    Code:
    if (arr[i][0] == 0) break;
    but i thought that maybe there is a better way.


    Best regards

  5. #5
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Well, you can do:
    Code:
    sizeof arr / sizeof arr[0]
    To work out the number of rows, if that's what you mean.

  6. #6
    Registered User
    Join Date
    Jan 2006
    Posts
    6
    What i want is to get to know number x from array[x][y]

  7. #7
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Quote Originally Posted by krST
    What i want is to get to know number x from array[x][y]
    look CWR post, to get the value of 'x' thats is no of rows.

    ssharish2005

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  4. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  5. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 09:16 PM