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
  • 19,824
  • 17
  • 99
  • 186
SKD
  • 464
  • 1
  • 4
  • 16
  • 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 `malloc`ing these things until you can write the code without it. – molbdnilo Sep 14 '15 at 09: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? – SKD Sep 14 '15 at 09: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`. – molbdnilo Sep 14 '15 at 09:24
  • I can write both programs without using malloc. But this malloc thing has ruined my mind. :( – SKD Sep 14 '15 at 09:24
  • in 2> when i omit the malloc line, its working fine. how? – SKD Sep 14 '15 at 09:29
  • 1
    @Sujit `array[nrows][ncolumns]` is an element outside the array; assigning to it is undefined. – molbdnilo Sep 14 '15 at 09:30
  • An option not mentioned yet is `int (*array)[ncolumns] = malloc(nrows * ncolumns * sizeof(int));` – M.M Sep 14 '15 at 10:04
  • @M.M , I have tried that option 3hrs ago. didn't work. May be my method was wrong. can u elaborate it? – SKD Sep 14 '15 at 10:13
  • @Sujit there's not any more to it really. Use it the same as if you had written `int array[nrows][ncolumns];`. post the code that isn't working if you have trouble – M.M Sep 14 '15 at 10:13
  • @M.M I just replaced malloc line with:- int (*array)[ncolumns] = malloc(nrows * ncolumns * sizeof(int)); Rest all lines were the same as posted in the question. – SKD Sep 14 '15 at 10:56
  • 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]);` – M.M Sep 14 '15 at 21:45

1 Answers1

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;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
  • Thanks a lot. It really helped. Though, in 1st program, M still getting the 'Type conversion' error.. – SKD Sep 14 '15 at 10:05
  • 1
    @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));` – BLUEPIXY Sep 14 '15 at 10:12
  • I am using Dev-C++ compiler. Thanks once again. Now both progrmas are running well. – SKD Sep 14 '15 at 10:23
  • Means? Where to add & which option? – SKD Sep 14 '15 at 10:44
  • @Sujit add example of `3> option`. M.M's comment for `int (*array)[ncolumns] ` – BLUEPIXY Sep 14 '15 at 10:48
  • Sorry, I didn't notice your edit. 3rd option is also working. (though it confused me a lot ). I have one question, why do we need typecasting before malloc here? – SKD Sep 14 '15 at 11:09
  • @Sujit because C++ isn't allow implicit conversion from `void *`. – BLUEPIXY Sep 14 '15 at 11:13
  • For allocating `int (*array)[nrows][ncolumns];` why not just do `array = malloc(sizeof *array);`? – alk Sep 14 '15 at 15:48
  • All these casts are not needed nor recommended in C: http://stackoverflow.com/q/605845/694576 – alk Sep 14 '15 at 15:51
  • _why not just do..._ It did not change so as not to unnecessarily confusing. (at option do it) – BLUEPIXY Sep 14 '15 at 15:55
  • @alk Thats not working. Shows error: Invalid conversion. – SKD Sep 15 '15 at 04:51
  • Ok, so you use a C++ compiler, why do you tag your question C though? C and C++ are *not* the same language. – alk Sep 15 '15 at 06:16
  • @alk yes I am using Dev C++ compiler , but my code is in C language not C++ . – SKD Sep 16 '15 at 08:26
  • If you use C++ compiler to compile code is is treated as C++ code, along with all issues popping up being C++ issues (by definition). To have the code treated a C code *use a C compiler*. – alk Sep 16 '15 at 10:01