开发者

Can I access type int (*)[] with [][]?

coming from this question "What does (int (*)[])var1 stand for?" I tried to access the result of the cast like a multidimensional array. But I get following error: "assignment from incompatible pointer type" followed by a segmentation fault. I tried also some other variations, but none of them worked. How can I access the elements in var1 in the function example directly?

Thank you!

 #include <stdlib.h>

int i(int n,int m,int var1[n][m]) {
  var1[0][0]=5;
  return var1[0][0];
}

int example() {
  int *var1 = malloc(100);

  // works
  int var2;
  var2 = i(10,10,(int (*)[])var1);
  printf("var2=%i",var2);
  //doesn't work I
  int *var3;
  var3=(int (*)[])var1; //"assignment from incompatible pointer type"
  printf("var3[0][0]=%i",var3[0][0]);


  //doesn't work II
  int *var4;
  var4=var1;
  printf("var4[0][0]=%i",var4[0][0]); //" error: subscripted value 开发者_如何学Cis neither array nor pointer"

  //doesn't work III
  int **var5;
  var5=var1;
  printf("var5[0][0]=%i",var5[0][0]); // assignment from incompatible pointer type


  return(1); 

} 

int main(){
  int a;
  a=example();
  return(1);
}


int *var3;
var3 = (int (*)[])var1;

You are casting var1 which is already int* to int(*)[] (pointer to array of int) and assigning it to var3 which again is int*.

Just do

var3 = var1


Give this a shot. The following compiled with no warnings and ran under C99 (gcc -std=c99 -pedantic -Wall):

#include <stdio.h>
#include <stdlib.h>

int i(int n, int m, int (*var1)[m]) // C89 requires constant expression for
{                                   // array size
  int j, k;
  for (j = 0; j < n; j++)
    for (k = 0; k < m; k++)
      var1[j][k] = j*m+k;
  return var1[0][0];
}

int example(void)
{
  int *var1 = malloc(100 * sizeof *var1);   // Thanks, Joseph!
  int var2 = i(10, 10, (int (*)[10]) var1); // note the cast of var1 includes
                                            // the array size
  int (*var3)[10] = (int (*)[10]) var1;     // note the type of var3

  int j, k;
  for (j = 0; j < 100; j++)
    printf("var1[%2d] = %d\n", j, var1[j]);

  for (j = 0; j < 10; j++)
    for (k = 0; k < 10; k++)
      printf("var3[%2d][%2d] = %d\n", j, k, var3[j][k]);

  free(var1);
  return var2;
}

int main(void)
{
  int x = example();
  printf("x = %d\n", x);
  return 0;
}

First of all, note the types and the casts (most importantly, note how they match up). Note that I am specifying the size of the array dimension in the pointer-to-array casts. Also note that I declare var3 as a pointer to an array, not a simple pointer.


int example() {
    int *var1 = malloc(100);

    ...

    int *var3;
    var3=var1;
    printf("var3[0][0]=%i",var3[0][0]); //" error: subscripted value is neither array nor pointer"

    return(1); 
} 

Here, var1 and var3 are both of type int*, which is roughly analogous to int[]. You've created a one-dimensional array and are trying to access them as a two-dimensional array. Change their type to int**, allocate the necessary memory, and that should fix your problem.


var3 needs to be a int** instead of an int*.

Edit

You're trying to use 2D array syntax where the actual data that you've created is actually a 1D array. You can use your i() function to give you the semantics you want but the data access needs to be converted to 1D indexing inside the function. Just make your function look like this:

int i(int n,int m,int* var1, int maxM) {
  return var1[(maxM * n) + m];
}


Perhaps what you are looking for is:

int (*var1)[10][10] = malloc(sizeof *var1); // 400 bytes
i(10, 10, *var1);
printf("var1[0][0]=%i\n", (*var1)[0][0]);

Added: A complete code fragment for gcc might be:

#include <stdio.h>
#include <stdlib.h>

void i(int n, int m, int var1[][m]) { // n not needed
    var1[0][0] = 5;
    var1[1][2] = 6;
}

int main(void) {
    int n = 10, m = 10;
    int (*var1)[n][m] = malloc(sizeof *var1); // 400 bytes
    i(n, m, *var1);
    printf("var1[0][0]=%i\n",(*var1)[0][0]);
    printf("var1[1][2]=%i\n",(*var1)[1][2]);
    return 0;
}

For msvc, you'll need to make n and m constants, as in:

enum {n = 10, m = 10};

void i(int var1[][m]) {
    var1[0][0] = 5;
    var1[1][2] = 6;
}

int main(void) {
    int (*var1)[n][m] = malloc(sizeof *var1);
    i(*var1);
    // ...
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜