creating 5X3 array using double astrix

This is a discussion on creating 5X3 array using double astrix within the C Programming forums, part of the General Programming Boards category; *p is the 0 row *p+1 is the first row etc.. and i apply to each row a column why ...

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    creating 5X3 array using double astrix

    *p is the 0 row
    *p+1 is the first row
    etc..
    and i apply to each row a column

    why its not working
    ??
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    	int i=0;
        int **p=(int**)malloc(5*sizeof(int*));
    	for (i=0;i<5;i++)
    	{
    	   *p+i=(int*)malloc(3*sizeof(int));
    
    	}
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Nov 2004
    Location
    India
    Posts
    515
    Change that to
    Code:
    p[i] = malloc ...
    Don't cast the return value of malloc.
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    Looks like you forgot the parenthesis (in red).
    Quote Originally Posted by transgalactic2 View Post
    *p is the 0 row
    *(p+1) is the first row
    etc..
    and i apply to each row a column

    why its not working
    ??
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    	int i=0;
        int **p=(int**)malloc(5*sizeof(int*));
    	for (i=0;i<5;i++)
    	{
    	   *(p+i)=(int*)malloc(3*sizeof(int));
    
    	}
    	return 0;
    }

  4. #4
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    Quote Originally Posted by itCbitC View Post
    Looks like you forgot the parenthesis (in red).
    so this code could be interpretative as p[3][5] and as p[5][3]
    ??

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    Quote Originally Posted by transgalactic2 View Post
    so this code could be interpretative as p[3][5] and as p[5][3]
    ??
    According to the storage allocations it can be interpreted only as p[5][3].

  6. #6
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    as i see it
    we can say *(p+1) is the first column instead of
    *(p+1) is the first row

    how the storage allocation contradicts it?

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What you intepret as column or row is entirely up to you. However, the dimensions that you allocate is correct for:
    Code:
    p[5][3];
    but not for
    p[3][5];
    [/code]
    as you would walk outside the memory allocated here:
    Code:
    malloc(3*sizeof(int))
    if you go beyond 2 in the second index.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    *(*(p+2)+1) equals p[2][1]
    ?

  9. #9
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    Quote Originally Posted by transgalactic2 View Post
    *(*(p+2)+1) equals p[2][1]
    ?
    Reading it from the inside out might help:

    p: pointer to pointer to int ie &p[0]
    p+2: points 2 elements past p ie &p[2]
    *(p+2): dereferences to give p[2]
    *(p+2)+1: points to the 2nd element of p[2] ie &p[2][1]
    *(*(p+2)+1): dereferences to give p[2][1]

  10. #10
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    thanks

  11. #11
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by transgalactic2 View Post
    *(*(p+2)+1) equals p[2][1]
    ?
    for arrays
    *(p+i)=p[i]=i[p]=*(i+p)
    *(*(p+i)+j)=p[i][j]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. functions and passing data
    By redmondtab in forum C Programming
    Replies: 41
    Last Post: 09-21-2006, 01:04 PM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM
  5. Creating array from data file...
    By Crankit211 in forum C Programming
    Replies: 2
    Last Post: 12-06-2001, 09:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21