Thread: Array Pointers error

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by taurus View Post
    well yea i get the error:
    multi.c:10: warning: assignment from incompatible pointer type

    I thought since its a 2D array i need a pointer to a pointer
    In C, many warnings are actually errors.
    And a good programmer never ever ignores a warning. A good programmer writes code in such a way that it does not create warnings, hence making it more "correct" (don't just silence warnings).

    Quote Originally Posted by taurus View Post
    PS: u said int** ptr. Is that the same as int **ptr?
    thanks
    Yes!
    http://www.research.att.com/~bs/bs_faq2.html#whitespace

    But just think of it this way:
    The compiler use a formula to calculate an offset to read/write to/from in an array.
    This formula looks like: inner_n * outer_size * sizeof(array_type) + outer_n * sizeof(array_type), where inner_n is the element you wish to access in the first dimension and outer_n the same, but for the outer dimension.
    An example:

    int n[10][20];
    n[5][6] = 1;

    The formula becomes:
    inner_n = 5
    outer_size = 20
    outer_n = 6
    5 (inner_n) * 20 (outer size) * sizeof(int) + 6 (outer_n) * sizeof(int)

    ...But if you pass a ptp (pointer to pointer), the formula becomes:

    int** n;
    n[5][6] = 1;

    Formula:
    inner_n = 5
    outer_size = ?
    outer_n = 6
    5 * ? * sizeof(int) * 6 * sizeof(int)

    As you can see, a variable is missing and therefore the calculation cannot be done.
    Therefore, a 2D array is not a ptp, it is instead a pointer which incorporate the size of the outer dimension:
    int (*n)[20];
    n[5][6] = 1;

    inner_n = 5
    outer_size = 20
    outer_n = 6
    5 * 20 * sizeof(int) + 6 * sizeof(int)
    Last edited by Elysia; 10-25-2008 at 11:41 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #17
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    But if you look at the code on the previous page whats wrong with it?

  3. #18
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The code is horribly wrong and crashes, and you should get several warnings, if not errors.
    First error is:

    Code:
    function(array, 4);
    array is not a ptp, it is a 2D array, and should NOT be passed as a ptp.
    Because arrays are contiguous, it's one big block of memory and the compiler uses the formula I outlined below. Therefore, if anything, it should be a pointer to int, only.

    The second and most horrifying blunder is:

    Code:
    ptr = array[0][0];
    You assign the value of the element 0, 0 to the pointer, which is 1. And 1 is not a valid address!
    Anything after this is guaranteed to go horrible and utterly wrong.

    Secondly, this part:
    Code:
    printf("%d\n", *(*(ptr + i) + j) );
    The value you're trying to get is horribly wrong, too, because you dereference twice. There are no pointers stored in the array, so you'll dereference a value and get a faulty memory address.
    Last edited by Elysia; 10-25-2008 at 12:20 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #19
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    Yea i do, but i dont know how to fix that given that I have to use the function declaration as seen with then **array

    What do i change?

  5. #20
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You should understand that. It has already been explained how to pass a 2D array. Start with that.
    Then print the array as you would a local array.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #21
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    Ok ummm
    since my function is
    void function(int **array, int num)

    That means array is a pointer to a pointer correct?

    so to pass an array to that i need to pass a pointer to a pointer to my array?
    Last edited by taurus; 10-25-2008 at 12:42 PM.

  7. #22
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If you can change that function, then do it. Use a proper type to pass the 2D array.
    You've been given several examples.
    Otherwise you need to pass the address of a pointer (or several), that finally points to your array.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #23
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    Code:
    #include <stdio.h>
    void function(int **array, int num)
    {
    	int i, j;
    	for(i=0; i<=1; i++)
    	{
    		for(j=0; j<=3; j++)
    		{
    			printf("&#37;d\n", array[i][j]);
    		}
    	}
    }
    
    int main(void)
    {
    	int i, j;
    
    	int array[2][4] = {{1,2,3,4}, {5,6,7,8}};
    	function(&array, 4);	
    }
    I did that but still doesnt seem to be happy with the passing of the array?

  9. #24
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Whoops! I was looking at the wrong page...anyway...

    Again, your use of a pointer to point to a pointer inside "function" is superfluous. I noticed that the rules governing char's and int's are slightly different here:

    Code:
    #include <stdio.h>
    void function(int (*array)[4])
    	{
    		int i, j;
    		for(i=0; i<=1; i++)
    		{
    			for(j=0; j<=3; j++)
    			{
    				printf("&#37;d\n", array[i][j] );
    			}
    		}
    	}
    
    void funchar(char **array) 
    	{
    		int i, j;
    		for(i=0; i<=1; i++)
    		{
    			for(j=0; j<=3; j++)
    			{
    				printf("%c\n", array[i][j] );
    			}
    		}
    }
    
    int main(void)
    {
    	int i, j;
    	char *ray[2] = {"this","that"};
    	int array[2][4] = {{11,22,33,44}, {55,66,77,88}};
    	function(array);	
    	funchar(ray);	
    }
    Output:
    11
    22
    33
    44
    55
    66
    77
    88
    t
    h
    i
    s
    t
    h
    a
    t
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #25
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    But iv been given a question in which i have to use the void function(int **array, int num)
    So with that in mind what do i do

  11. #26
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Since T* is what you get what you take the address of T, then to get T**, you would need to take the address of T*. What do you get when you take the address of a 2D-array?
    You get T* (*)[inner_size][outer_size], so no, that is horribly incorrect.

    However, what you can do is use the dynamic approach of creating a 2D array.
    First you create an array of pointers.
    Each of those pointers will pointer to an element.
    That way you get T** for the basic array, T* for each pointer to T, and T for each element.

    It may be difficult, but if you can crack this nut with the information given, then you should become a good programmer.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #27
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    hmm so you mean something like this:
    Code:
    nt main(void)
    {
    	int i, j;
    	int (*ptr)[2];
    	ptr[4] = {{1,2,3,4}, {5,6,7,8}};
    	function(ptr);	
    }

    yep true, pointers are the hardest part i heard, so i hope i can crack this, persistence should pay off i hope

  13. #28
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by taurus View Post
    hmm so you mean something like this:
    Code:
    nt main(void)
    {
    	int i, j;
    	int (*ptr)[2];
    	ptr[4] = {{1,2,3,4}, {5,6,7,8}};
    	function(ptr);	
    }

    yep true, pointers are the hardest part i heard, so i hope i can crack this, persistence should pay off i hope
    We mean something maybe a little bit like that. For sure, malloc will be involved. To get an m x n array, your **array would have to hold m single-star pointers; and then each single-star pointer will have to hold n integers.

  14. #29
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Try to think visually!
    Perhaps this might be of a little help: http://cboard.cprogramming.com/showp...3&postcount=31
    Remember that ptp is an address of a pointer to T.

    Code:
    int x;
    int* p = &x;
    int** p2 = &p;
    Armed with this knowledge, perhaps it will become easier?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #30
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    Ill have a read thanks, its a lot clearer now then at the start of the day

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to monitor process creation?
    By markiz in forum Windows Programming
    Replies: 31
    Last Post: 03-17-2008, 02:39 PM
  2. using c++ in c code
    By hannibar in forum C Programming
    Replies: 17
    Last Post: 10-28-2005, 09:09 PM
  3. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  4. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM
  5. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM