1. Originally Posted by itCbitC
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.

2. 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. Originally Posted by itCbitC
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.

4. Originally Posted by iMalc
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.
Originally Posted by iMalc
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.
Originally Posted by iMalc
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]);
}```
Originally Posted by iMalc
I suggest studying and learning all three methods.
gotcha! O learned one

5. 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

6. 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.

7. Originally Posted by vart
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. Originally Posted by iMalc
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.