in the above matrix multiplication problem i find the product matrix and store the it in the memory location pointed by p. now i want to multiply each element in product by 50. i no longer need p but need only q. will it be ok if i declare q as

int **q;

without allocating separate memory for q??? will the elements of p in the corresponding location be replaced by elements of q???

Firstly, your code is terribly formatted! There are literally hundreds of extra carriage returns and the indenting is very irregular, making the code pretty much unreadable. Please show some effort and format you code in a sensible manner, rather than expect the reader to put in the extra effort to understand it. You will be much more likely to get a response that way. Also, have you tried to compile this code? I would say not, since there are a number of errors. For instance, you have used int **m = malloc(r*sizeof(int *)); which will not compile since, malloc() returns void * . You need to explicitly cast the return-type to int ** , like int **m = (int **)malloc(r*sizeof(int *)); . Also, in your function matrix_mul() you try and check for an error and then on finding it, you have return 1; , but 1 is of integer type, not int ** , which is what you declare the return-type of the function to be. You should at least try and compile your code! Incidentally, the standard way to do this kind of error-checking is to use the return value as an indicator of errors and pass a pointer to the variable that you want to put the result in.

To answer your question: why don't you just keep using p , but with the elements multiplied by 50? If you need to use a different variable name, you can just do int **q = p; and then use q as you would have p . This doesn't copy the values or anything though, you'll be writing over the old ones. Is that what you want?

I think this is only necessary in C++, in C there is an implicit conversion.

Ah, I see. Maybe it's not strictly necessary then, but it's probably good practice to specify the return type explicitly, unless there's a really good reason for wanting an implicit cast? If not, then I take back my comment in that respect. I think that my general point is still valid though.

thanks.. these replies helps a lot.. i understand the problems with the format i presented the post.. pardon me for mistakes...
1. so i dont have to allocate memory for q and only thing needed is to declare q as int (**q)???
2. also i can use p and q both the same time. am i right???
3.even without the int** before malloc, program works well. anyway, i will try to follow standard methods and will declare the return type also in my next program

thanks.. these replies helps a lot.. i understand the problems with the format i presented the post.. pardon me for mistakes...
1. so i dont have to allocate memory for q and only thing needed is to declare q as int (**q)???

You need to declare q as int ** so that the type matches with p . For q to do anything to the values that p points at, you need to aim it at the same piece of memory, so something like:

/* Make an array of ints */
int *p = (int *)malloc(10*sizeof(int *));
/* Set the values of the array (the numbers 0 - 9, in this case) */
for(int i = 0; i < 10; i++) p[i] = i;
/* Print the resulting array to the terminal */
printf("p = ");
for(int i = 0; i < 10; i++) printf("%d ", p[i]);
printf("\n");
/* Make a pointer and aim it at the same place as p */
int *q = p;
/* Now do some things to the values that the pointer is aiming at */
for(int i = 0; i < 10; i++) q[i] = 50*q[i];
/* Print out the new values */
printf("q = ");
for(int i = 0; i < 10; i++) printf("%d ", q[i]);
printf("\n");
/* Now check the old values */
printf("p = ");
for(int i = 0; i < 10; i++) printf("%d ", p[i]); // They've changed as well!
printf("\n");

2. also i can use p and q both the same time. am i right???

You can, but they're both pointing at the same thing, so using them both at the same time might be confusing.

Getting back to your original application (matrix multiplication), I think it's quite common to do this kind of thing "in place". That is you have a function like

The return value can be use to check for errors (eg. incompatible row/column sizes) and the result is written over the first matrix, and the new row/column sizes passed out to the those that were inputted as the sizes for the first matrix. This has exactly the effect that you're talking about, except that the data is written over the original data, and we just keep using the same pointer. For the exact same thing that you were talking about (scaling the elements of a matrix by a constant) you would use something like

I have a 2d matrix with dimension (3, n) called A, I want to calculate the normalization and cross product of two arrays (b,z) (see the code please) for each column (for the first column, then the second one and so on).
the function that I created to find the ...

Write a C program that should create a 10 element array of random integers (0 to 9). The program should total all of the numbers in the odd positions of the array and compare them with the total of the numbers in the even positions of the array and indicate ...