简体   繁体   English

对C指针的怀疑

[英]doubts about C pointers

I am trying to understand basic concepts in C. I have two questions : 我试图理解C中的基本概念。我有两个问题:

1.Why will the following piece of code work and the other does not? 1.为什么下面的代码会起作用而另一个代码不起作用?

/* This works fine */
typedef int SortTableRows[20]; 

SortTableRows* SortTableRowsPtr;

/* This will give error --> subscripted value is neither array nor pointer*/
int SortTableRows[20]; 

SortTableRows* SortTableRowsPtr;
  1. Second question is what is the third line of code doing? 第二个问题是第三行代码是做什么的?

     typedef int SortTableRows[20]; SortTableRows* SortTableRowsPtr; SortTableRowsPtr[2][3]=2; //Why the compiler doesnt give any error?how can we use a pointer as a 2d array. 

You've defined SortTableRows to be an array of ints with int SortTableRows[20] . 你已经使用int SortTableRows[20]SortTableRows定义为ints的数组。 That identifier is now fixed as an array, it cannot be used for anything else. 该标识符现在固定为数组,不能用于其他任何内容。 You're then trying to use it as a type to declare a pointer (I think). 然后你尝试将它用作声明指针的类型(我认为)。 If you want to declare a pointer to an array of ints you would do it like this: 如果你想声明一个指向int数组的指针,你会这样做:

int *SortTableRowsPtr;
SortTablesRowPtr = SortTablesRows;

In the second question, you're assigning a value to a pointer to a pointer that you're treating as a two dimensional array, which makes no real difference to your compiler (though it makes no real sense in this context), as 'multidimensional' arrays are stored in memory in a linear way, the same as single dimension arrays. 在第二个问题中,您将一个值赋给一个指向您正在将其视为二维数组的指针,这对您的编译器没有任何实际影响(尽管在此上下文中没有任何意义),因为'多维'数组以线性方式存储在内存中,与单维数组相同。 Your typedef doesn't make any real sense here, as you're defining a type of array, then making an uninitialised pointer to it (this creates an int ** ), and accessing it as if it points to something valid (which will compile, but would surely crash). 你的typedef在这里没有任何意义,因为你正在定义一种类型的数组,然后创建一个未初始化的指针(这会创建一个int ** ),然后访问它就像它指向有效的东西一样(这将是编译,但肯定会崩溃)。

To have this work you'd need to define an array of 20 ints and then point to it: 要完成这项工作,您需要定义一个包含20个整数的数组,然后指向它:

SortTablesRows table[10] = {};  // equivalent to int table[10][20];
SortTablesRowPtr = table;

This works because you're declaring a pointer to an array of 20 ints, ie 这是有效的,因为你声明了一个指向20个整数数组的指针,即

int (*SortTablesRowPtr)[20];

I've expanded this answer. 我已经扩展了这个答案。 It helps to explain it more simply step by step. 它有助于逐步解释它。 Take a table of 3 rows and 10 columns. 拿一张3行10列的表格。 That's defined: 那是定义的:

int table[3][10];

You can then set a pointer to this table. 然后,您可以设置指向此表的指针。 Because it's in two dimensions this isn't right: 因为它是二维的,所以这是不对的:

int *ptr = table; // wrong - incompatible pointer

But this would point to the second row (and the first column): 但这将指向第二行(和第一列):

int *ptr = table[1];

In other words you want a pointer to an array of 10 ints, ie you want a pointer that points to a type of int n[10] . 换句话说,你想要一个指向10个int数组的指针,即你想要一个指向int n[10]类型的指针。 To do this you can write: 要做到这一点,你可以写:

int (*ptr2)[10] = table;

Now you can use ptr2 to access the table directly, either as pointers, arrays, or both. 现在您可以使用ptr2直接访问表,无论是指针,数组还是两者。 As it stands it points to the first row and first column. 现在它指向第一行和第一列。 If you add one to ptr2 it will select the next row. 如果你向ptr2添加一个,它将选择下一行。

ptr2[1][3] = 3;  // change row 2, column 4
(*(ptr2 + 1))[4] = 10 // row 2, column 5 (yuck)

Question 1. 问题1。

In the first you are telling it that SortTablesRow is an array of 20 ints. 在第一个中,你告诉它SortTablesRow是一个20个整数的数组。 So SortTablesRow is a type. 所以SortTablesRow是一种类型。 In the second you are creating a variable called SortTablesRow which is an array of 20 ints. 在第二个中,您将创建一个名为SortTablesRow的变量,该变量是一个20个整数的数组。 This can't be used as a type because its a variable. 这不能用作类型,因为它是一个变量。

Question 2. 问题2。

Here you are typedef'ing SortTablesRow to be an array of 20 ints. 在这里,你输入的SortTablesRow是20个整数的数组。 You then say SortTablesRowPtr is a pointer to it. 然后你说SortTablesRowPtr是指向它的指针。 So now you have a pointer to an array of 20 ints. 所以现在你有一个指向20个int数组的指针。 This is essentially an int**. 这基本上是一个int **。 So the first array index is indexing the first pointer and the second array index is indexing the second pointer (or the array) and thus looking up in the 20 element int array. 因此,第一个数组索引索引第一个指针,第二个数组索引索引第二个指针(或数组),从而查找20个元素的int数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM