CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Mar 2003
    Posts
    29

    Pointer to array

    Hi,

    In the below program, while compiling I getting a warning
    " warning C4047: '=' : 'int (*)[10]' differs in levels of indirection from 'int *' ". But it works as expected. Also I dont want to change the declaration "pointer to array". How can I eliminate this? Please any one help.

    #include <stdio.h>
    main()
    {
    int (*n)[10];
    int i,ii;

    n=(int *)malloc(sizeof(int)*1024);

    for(i=0;i<10;i++)
    {
    for(ii=0;ii<10;ii++)
    {
    n[i][ii]=ii+i;
    }
    }

    for(i=0;i<10;i++)
    {
    for(ii=0;ii<10;ii++)
    {
    printf("%d\n",n[i][ii]);
    }
    }

    }

  2. #2
    Join Date
    Dec 2001
    Location
    Ontario, Canada
    Posts
    2,236
    An array is a pointer, there is no need for a declaration like yours. And why do you multiply sizeof int by 1024? If you want an array of ints, do this:

    Code:
    intmain()
    {
    int* array = NULL;
    array = (int*)malloc( sizeof(int)*10 ); // 10 elements of an array
    }
    if you want a 2 dimensional array, do this:

    Code:
    int main()
    {
    int** array;
    array = (int**)malloc( sizeof(int*) * 10 ); // 10 in pointers
    
    for( int i = 0;i < 10;i++ )
    array&#091;i&#093; = (int*)malloc( sizeof(int) * 10 ); // 10 ints
    }

  3. #3
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Is there any reason why you cannot use the standard vector template class? It is much easier than the old ANSI C arrays and it provides its own memory management.

    For an introduction to the vector class take a look at this article...

    Also...for the usage of vectors as two-dimensional arrays take a look at this FAQ...

  4. #4
    Join Date
    Oct 2001
    Location
    Dublin, Eire
    Posts
    880
    The fact is that your declaration: int (*n)[10]; is that of a two dimensional array. At the same time, your memory reservation is that of a one dimensional array: (int*).

    That is the cause of your problem.

    Why kind of array are you exactly trying to do?

    two dimensional: 10*N or N*10 ?

    Or is it something else.
    Elrond
    A chess genius is a human being who focuses vast, little-understood mental gifts and labors on an ultimately trivial human enterprise.
    -- George Steiner

  5. #5
    Join Date
    Mar 2003
    Posts
    29
    I am trying to have a N*10 array. Thanks for your interst.

    Thanks and Regards,
    Abbas

  6. #6
    Join Date
    Oct 2001
    Location
    Dublin, Eire
    Posts
    880
    Using new is usually better than malloc when coding in C++. Or are you coding in C?

    This works anyway:
    Code:
    int (*n)[10];
    UINT N = 7;
    n = new int[N][10];
    
    for (int i = 0; i < N; i++)
    {
    	for (int j = 0; j < 10; j++)
    	{
    		n[i][j] = i*j;
    		cout << n[i][j] << " ";
    	}
    	cout << endl;
    }
    
    delete [] n;
    Elrond
    A chess genius is a human being who focuses vast, little-understood mental gifts and labors on an ultimately trivial human enterprise.
    -- George Steiner

  7. #7
    Join Date
    Mar 2003
    Posts
    29
    I am working with C. I am stuck with C because I work in a API development of a CAD package which only supports C. Thanks you very much!

    Thanks and Regards,
    Abbas

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured