3

Below is the part of code I am stuck on. I want to dynamically allocate memory for

  • Pointer to array
  • Array of pointers

I am getting several error messages like invalid conversion from int * to int and so on.

Pointer to array

int (*array)[nrows][ncolumns];
array = (int*)malloc(nrows * ncolumns * sizeof(int));
printf("\n Enter the elements:\n");
for(i=0; i<nrows; i++)
{
 for(j=0; j<ncolumns; j++)
 {
 scanf("%d", array[i][j]); 
 }
}
printf("Entered array is :\n\n");
for(i = 0;i<nrows; i++)
{
 for(j = 0; j<ncolumns; j++)
 {
 if(j== ncolumns-1)
 {
 printf("%d \n", *array[i][j]);
 }
 else
 {
 printf("%d", *array[i][j]);
 }

Array of pointers

int *array[nrows][ncolumns];
array[nrows][ncolumns] = (int*)malloc(nrows * ncolumns * sizeof(int));
printf("Enter elements:\n");
for(i = 0; i<nrows; i++)
{
 for(j = 0; j<ncolumns;j++)
 {
 scanf("%d",&array[i][j]);
 }
}
printf("Entered array is: \n");
for(i = 0; i<nrows; i++)
{
 for(j = 0; j<ncolumns;j++)
 {
 if(j == ncolumns-1)
 {
 printf("%d \n",array[i][j]);
 }
 else
 {
 printf("%d \t",array[i][j]);
 }
 }
}
halfer
20.2k19 gold badges110 silver badges207 bronze badges
asked Sep 14, 2015 at 9:08
11
  • 2
    I recommend that you start with one-dimensional arrays until you're comfortable with the difference between arrays and pointers, and the difference beween a pointer to an array and a pointer to an array's first element. Also, that you stay away from mallocing these things until you can write the code without it. Commented Sep 14, 2015 at 9:17
  • Thanks. But actually I don't have that much time. I have to correct it in 1 hour. Please help me out. Is the memory allocation part correct? Commented Sep 14, 2015 at 9:21
  • 2
    @Sujit Well, you should have began studying earlier. Hints: In 1), array[i] is a two-dimensional array, array[i][j] is a one-dimensional array. In 2), array[i][j] is an (uninitialised) pointer, not an int. Commented Sep 14, 2015 at 9:24
  • 1
    @Sujit array[nrows][ncolumns] is an element outside the array; assigning to it is undefined. Commented Sep 14, 2015 at 9:30
  • 1
    @Sujit you also need to fix the bugs in your use of scanf and printf, it should be scanf("%d", &array[i][j]); and printf("%d", array[i][j]); Commented Sep 14, 2015 at 21:45

1 Answer 1

4

1> pointer to array

#include <stdio.h>
#include <stdlib.h>
int main(void){
 int nrows = 3;
 int ncolumns = 4;
 int i, j;
 int (*array)[nrows][ncolumns];//do you want <<int (*array)[ncolumns]>> ?
 //like as int src[nrows][ncolumns]; array = &src;
 array = malloc(nrows * ncolumns * sizeof(int));//(int*) : type mismatch
 printf("\nEnter the elements:\n");
 for(i = 0; i<nrows; i++){
 for(j = 0; j<ncolumns; j++){
 scanf("%d", &(*array)[i][j]);
 }
 }
 printf("Entered array is :\n\n");
 for(i = 0; i<nrows; i++){
 for(j = 0; j<ncolumns; j++){
 if(j != 0)
 putchar(' ');
 printf("%d", (*array)[i][j]);//need ( )
 }
 putchar('\n');
 }
 free(array);
 return 0;
}

2> Array of pointers

#include <stdio.h>
#include <stdlib.h>
int main(void){
 int nrows = 3;
 int ncolumns = 4;
 int i, j;
 int *array[nrows][ncolumns];
 int *src = (int*)malloc(nrows * ncolumns * sizeof(int));//no need (int*)
 printf("Enter elements:\n");
 for(i = 0; i<nrows; i++){
 for(j = 0; j<ncolumns;j++){
 array[i][j] = &src[ i * ncolumns + j];//pointer pointed to entity (src[ i * ncolumns + j])
 scanf("%d", array[i][j]);//type of array[i][j] is int *
 }
 }
 printf("Entered array is: \n");
 for(i = 0; i<nrows; i++){
 for(j = 0; j<ncolumns; j++){
 if(j != 0)
 putchar(' ');
 printf("%d", *array[i][j]);//need * for dereference
 }
 putchar('\n');
 }
 free(src);
 return 0;
}

3> option

#include <stdio.h>
#include <stdlib.h>
int main(void){
 int nrows = 3;
 int ncolumns = 4;
 int i, j;
 int (*array)[ncolumns];
 array = (int (*)[ncolumns])malloc(nrows * sizeof(*array));//sizeof(*array) : sizeof(int[ncolumns])
 printf("\nEnter the elements:\n");
 for(i = 0; i<nrows; i++){
 for(j = 0; j<ncolumns; j++){
 scanf("%d", &array[i][j]);
 }
 }
 printf("Entered array is :\n\n");
 for(i = 0; i<nrows; i++){
 for(j = 0; j<ncolumns; j++){
 if(j != 0)
 putchar(' ');
 printf("%d", array[i][j]);
 }
 putchar('\n');
 }
 free(array);
 return 0;
}
answered Sep 14, 2015 at 9:55
Sign up to request clarification or add additional context in comments.

14 Comments

Thanks a lot. It really helped. Though, in 1st program, M still getting the 'Type conversion' error..
@Sujit Perhaps you are using C++ compiler as C compiler. (cast from void * no need in C ), try array = (int (*)[nrows][ncolumns])malloc(nrows * ncolumns * sizeof(int));
I am using Dev-C++ compiler. Thanks once again. Now both progrmas are running well.
Means? Where to add & which option?
@Sujit add example of 3> option. M.M's comment for int (*array)[ncolumns]
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.