Thread: pointer arrays!!!

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    80

    Post pointer arrays!!!

    i have a problem:

    *p[5] means p[5][5]?

    and i think i have to give a value to first pointer

    how can i do it?

    my last question is i have a 5x5 matrix how can i use this as a pointer *p[5] or *p[5][5]? and how can i give values to matrix?

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: pointer arrays!!!

    >*p[5] means p[5][5]?
    No. The first is an array of 5 pointers. The second is a 2d array (5x5) of whatever type the variable is declared.
    To complete the declarations:
    >int *p[5];
    >int p[5][5];
    With the first you only get pointers to ints (5 of them).
    With the second, you get a 5x5 *grid* of ints.

    >and i think i have to give a value to first pointer
    If you want to use it, you have to assign the pointer a value.

    >how can i do it?
    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	int i;  /* one int */
    	int *p[5]; /* 5 pointers to ints */
    
    	p[0] = &i; /* assign the address of i to the first int pointer in the array */
    
    	i = 1; /* set i to 1 */
    	printf("i is %d\n", i);  /* show i */
    
    	*p[0] = 2;  /* change the data located at the address the pointer is pointing to */
    	printf("i is %d\n", i);  /* Show i again */
    	return (0);
    }
    
    Output:
    i is 1
    i is 2
    >my last question is i have a 5x5 matrix how can i use this as a pointer *p[5] or *p[5][5]? and how can i give values to matrix?
    Have a look at this modified version of my first example:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	/*~~~~~~*/
    	int i[] = {1,2,3,4,5};
    	int *p[5];
    	/*~~~~~~*/
    
    	p[0] = &i[0];  /* can be simplified as p[0] = i; */
    
    	printf("i[0] is %d\n", i[0]);
    
    	*p[0] = 2;
    	printf("i[0] is now %d\n", i[0]);
    	return (0);
    }
    This doesn't use all 5 element of the pointer array (p), it just shows how to use 1 element. You can expand on this in your own code.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. pointer arrays
    By condorx in forum C Programming
    Replies: 3
    Last Post: 05-03-2002, 09:04 PM
  5. Replies: 4
    Last Post: 11-05-2001, 02:35 PM