Thread: multidimensional array help

  1. #16
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by itCbitC View Post
    Ah! yes I forgot to square the array rank while malloc()'ing memory so here's the one w/o the typo.
    Code:
    p=malloc((n*n)*sizeof(int*));	
    for(i=0;i<n*n;i++)
    {
        p[i]=(int*)malloc(sizeof(int));	
        scanf("%d",p[i]);	
        *p[i] *=2;		
    }
    The rest stays as is because this is a 2D array so p[i] == &p[i][j] except if it were a 3D.
    No NO NO! Completely wrong again!
    You do not have a malloc in the loop for this style of array, and p[i] is never the same as &p[i][j]!
    Don't post any more code unless you've tested it first okay? You're leading people astray.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  2. #17
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    IBTD the code works and you need to malloc() inside the loop for storing the ints that are going to be scanned in. For the rest what I meant when I said that
    Code:
    p[i] = &p[i][j];
    was really meant to be
    Code:
    &p[i][j] = p[n*i + j];
    Ultimately let the o/p to decide what style to use - array subscripting for clarity or pointer and offset for creating concise code.
    Just my 2c

  3. #18
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by itCbitC View Post
    IBTD the code works and you need to malloc() inside the loop for storing the ints that are going to be scanned in.
    And you've been programming professionally using C or C++ on a daily basis for how many years?
    You're in a hole - stop digging.

    Using the "n*i + j" method requires one malloc call, and the type of p is int*.
    Here are two ways you can do it other than the array of pointers way the OP used:
    Treating it entirely as a 1D array
    Code:
    int *p = malloc(n*n*sizeof(int));	
    for (i=0; i<n*n; i++)
    {
        scanf("%d", &p[i]);	
        p[i] *= 2;		
    }
    Treating it the bitmap way
    Code:
    int *p = malloc(n*n*sizeof(int));	
    for (i=0; i<n; i++)
        for (j=0; j<n; j++)
        {
            scanf("%d", &p[i * n + j]);	
            p[i * n + j] *= 2;		
        }
    You'll notice that this first one is identical to what I posted earlier, and is correct.

    And for completeness, the array of pointers method:
    Code:
    int **p = malloc(n*sizeof(*p));	
    for (i=0; i<n; i++)
    {
    	p[i] = malloc(n*sizeof(int));	
    	for (j=0; j<n; j++)
    	{
    		scanf("%d", &p[i][j]);	
    		p[i][j] *= 2;		
    	}
    }
    I suggest studying and learning all three methods.
    Last edited by iMalc; 02-16-2009 at 11:19 PM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #19
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by iMalc View Post
    And you've been programming professionally using C or C++ on a daily basis for how many years?
    I am still learning - a singular aspect that never changes.
    Quote Originally Posted by iMalc View Post
    You're in a hole - stop digging.
    I'm holed up only because you have tunnel vision.
    In any case let's pick a method you posted and talk about it. I picked the one below as it is closest to the solution the O/P wanted. The code allocates storage for an array[] of n pointers to int; each of those pointers in turn point to an array[n] of ints.
    Quote Originally Posted by iMalc View Post
    Code:
    int **p = malloc(n*sizeof(*p));	
    for (i=0; i<n; i++)
    { 
    	p[i] = malloc(n*sizeof(int));	
    	for (j=0; j<n; j++)
    	{
    		scanf("%d", &p[i][j]);	
    		p[i][j] *= 2;		
    	}
    }
    Your method is correct as is mine and for the sake of completeness here's the entire code; I suggest you try it out before jumping to any conclusions.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
        int i, j, n;
        printf("n = ");
        scanf("%d", &n);
        int **p = (int**)malloc((n*n)*sizeof(*p));
    
        for (i=0; i<n*n; i++) {
            p[i] = (int*)malloc(sizeof(int));
            scanf("%d",p[i]);
            *p[i] *= 2;
        }
        for (i=0; i<n*n; i++)
            printf("*p[%d]=%d\n",i,*p[i]);
    }
    Quote Originally Posted by iMalc View Post
    I suggest studying and learning all three methods.
    gotcha! O learned one

  5. #20
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Your method is correct as is mine
    You have the right to beleive in this. The problem is - your wrong beleives will prevent you from learning the right way to do things.

    We cannot tutor someone who does not want to learn. We just ask not to post more nonsence posts to prevent misguiding other people here
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #21
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    itCbitC was proposing a piece of code as though it were a simplification of the OP's code. This claim, which is mainly what I was disputing, was and still is false. The OP was using an array of pointers to an array, hence the need for two []'s.

    itCbitC's code should compile, but it is doing the unthinkable. It's a 1D array of pointers where each pointer points to an individual int. One would never use this in the real world, and it should never have been suggested as a viable option. That code is so far off from actually being useful, that I was neglecting to consider that it could even be compiled, and the initial code posted by itCbitC had bugs anyway.
    My appologies to itCbitC for not realising that his code could even compile, I'll give him/her that. Just don't expect any points for finding the worst way possible of doing something.
    Last edited by iMalc; 02-18-2009 at 06:14 AM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #22
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by vart View Post
    You have the right to beleive in this. The problem is - your wrong beleives will prevent you from learning the right way to do things.

    We cannot tutor someone who does not want to learn. We just ask not to post more nonsence posts to prevent misguiding other people here
    I apologize if I came across as misleading, that's not what I intended. I did write the code in a hurry and posted it w/o testing.

  8. #23
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by iMalc View Post
    itCbitC was proposing a piece of code as though it were a simplification of the OP's code. This claim, which is mainly what I was disputing, was and still is false. The OP was using an array of pointers to an array, hence the need for two []'s.

    itCbitC's code should compile, but it is doing the unthinkable. It's a 1D array of pointers where each pointer points to an individual int. One would never use this in the real world, and it should never have been suggested as a viable option. That code is so far off from actually being useful, that I was neglecting to consider that it could even be compiled, and the initial code posted by itCbitC had bugs anyway.
    My appologies to itCbitC for not realising that his code could even compile, I'll give him/her that. Just don't expect any points for finding the worst way possible of doing something.
    Well looks like we are not on the same page as I was simply refuting the argument that the posted code is wrong ie not compilable. The code you posted esp. the last one is the best option as it truly creates a nxn matrix of ints while mine creates a n*nx1 matrix. Please don't read between-the-lines as there ain't any. There's one more thing wrong with it but since you have gone over it with a fine-tooth comb, I'll let you figure it out as I won't buy the argument that it is not viable unless backed by proof instead of rhetoric.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Help -- allocating memory in a multidimensional array
    By jonathan.plumb in forum C Programming
    Replies: 10
    Last Post: 05-20-2009, 11:04 PM
  3. Pointer to multidimensional array
    By ejohns85 in forum C++ Programming
    Replies: 4
    Last Post: 03-24-2009, 11:17 AM
  4. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM

Tags for this Thread