0

Hello there I am having trouble declaring two dimensional arrays using pointers. I am new to C.

int main(){

    int x = 0;
    int y = 0;

    int *xdim;
    int *ydim;


    printf("Enter x:\n");
    scanf("%d", &x); 
    printf("Enter y:\n");
    scanf("%d", &y);

    xdim = &x;
    ydim = &y;

    char sq[xdim][ydim];

    return 0;
}

I want the two dimentionial array char sq to hold the values inputed by the user. I get error non-integer type *int. I am also new to pointers.

4
  • Guys sorry for my answer. I'm getting crazy getting a dll work. Sorry @Anonymous Penguin my answer was wrong ... Commented Jan 17, 2016 at 18:01
  • 1
    Just use char sq[x][y];, you do not need xdim/ydim Commented Jan 17, 2016 at 18:02
  • @Ctx he just wants to test how pointers work. Use char sq[*xdim][*ydim] Commented Jan 17, 2016 at 18:03
  • They are initialized xdim = &x if scanf doesn't return error. Commented Jan 17, 2016 at 18:05

5 Answers 5

1

Using VLAs (which are allowed in C99, or as an extension to many compilers), all you need to do is:

int xdim, ydim;
scanf("%d %d", &xdim, &ydim);

char vla_array[xdim][ydim];

As you can see, there is no need for pointers.


The 2nd option is to use "ordinary" arrays. You need to declare an array with big enough size, and use only a part of it:

#DEFINE X_MAX_DIM 100
#DEFINE Y_MAX_DIM 100

char vla_array[X_MAX_DIM][Y_MAX_DIM];

int xdim, ydim;
scanf("%d %d", &xdim, &ydim);
// check xdim and ydim are acceptable sizes

// from now on you use up to `xdim x ydim` of your array, even if
// it was declared as `100x100`

You can use this only when you know in advance the maximum allowed dimensions of your array.


The 3rd option is to use dynamic allocation. This is the only option if the array is very big. However this is an advanced topic, so you will not deal with this right now.

Sign up to request clarification or add additional context in comments.

9 Comments

You don't need to mention VLA as though they're something unusual. They are a core part of C. OP is using C, therefore they can use VLA. If they can't use VLA, they should specify that they are using C89, or a compiler that has chosen to use the no-VLA option, not pure C. (Not providing VLA is a permitted extension. By default, they are assumed to be allowed.)
@Leushenko I think I was pretty clear as when are VLA accepted.
@Leushenko Using VLA's is still a choice, for example they should be avoided for huge arrays.
It's a linguistic issue: "if you are OK with..." implies there might be something non-standard about them, and there's certainly no need to mention extensions. New C users shouldn't be discouraged from using standard features.
@Leushenko ok, I see your point. I removed the "if you are OK" part, but I left the "C99 and extension" info.
|
1
int** ptr = NULL;
ptr = new int*[val];

for(int i=0; i < val; i++)
{
    ptr[i] = new int[val];
}

above code allocates 2d array of val*val

Comments

0

change char sq[xdim][ydim]; to char sq[*xdim][*ydim];, else you pass the location of the value rather than the value itself.
But it is unnecessary to use pointers in this case, you could just use x and y.

Comments

0

In the code you sowed the error occured because in this declaration

char sq[xdim][ydim];

there are used pointers xdim and ydim instead of integers. You could write

char sq[*xdim][*ydim];

Though there is no need to use pointers such a way.

If your compiler supports Variable Length Arrays then you can for example just write

int main( void )
{
    size_t m = 0;
    size_t n = 0;

    printf("Enter x:\n");
    scanf("%zu", &m); 
    printf("Enter y:\n");
    scanf("%zu", &n);

    if ( !m || !n ) return 1;

    char sq[m][n];

    //...     

    return 0;
}

Otherwise you have to allocate arrays dynamically. For example

#include <stdlib.h>

int main( void )
{
    size_t m = 0;
    size_t n = 0;

    printf("Enter x:\n");
    scanf("%zu", &m); 
    printf("Enter y:\n");
    scanf("%zu", &n);

    if ( !m || !n ) return 1;

    char **sq = malloc( m * sizeof( char * ) );

    for ( size_t i = 0; i < m; i++ ) sq[i] = malloc( n * sizeof( char ) );

    //...

    for ( size_t i = 0; i < m; i++ ) free( sq[i] );
    free( sq );

    return 0;
}

Comments

0

I believe if i understand what you're asking this should work.

Using a single pointer within a 2D array;

#include <stdio.h>
#include <stdlib.h>
int x = 0, y = 0;
char *arr = (char *)malloc(x * y * sizeof(int));

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.