[英]What is the difference between these access in C?
i started learning C today, and i have some questions about accessing a pointer data.今天开始学习C,对访问一个指针数据有一些疑问。
I have this function in C:我在 C 中有这个 function:
typedef struct
{
size_t size;
size_t usedSize;
char *array;
} charList;
void addToCharList(charList *list, char *data)
{
if(list->usedSize == list->size)
{
list->size *= 2;
list->array = realloc(list->array, list->size * sizeof(int));
}
list->array[list->usedSize++] = *data;
printf("1: %d\n", *data);
printf("2: %s\n", data);
printf("3: %p\n", &data);
}
I use it to create a "auto growing" array of chars, it works, but i'm not understanding why i need to attribute the value "*data" to my array.我用它来创建一个“自动增长”的字符数组,它有效,但我不明白为什么我需要将值“*data”归因于我的数组。 I did some tests, printing the different ways i tried to access the variable "data", and i had this outputs (I tested it with the string "test"):我做了一些测试,打印了我尝试访问变量“data”的不同方式,我得到了这个输出(我用字符串“test”测试了它):
1: 116
2: test
3: 0x7fff0e0baac0
1: Accessing the pointer (i think it's the pointer) gives me a number, that i don't know what is. 1:访问指针(我认为是指针)给了我一个数字,我不知道是什么。
2: Just accessing the variable gives me the actual value of the string. 2:只要访问变量就可以得到字符串的实际值。
3: Accessing it using the "&" gets the memory location/address. 3:使用“&”访问它会得到 memory 位置/地址。
When i'm attributing the value of my array, i can only pass the pointer, why is that?当我为我的数组赋值时,我只能传递指针,这是为什么? Shouldn't i be attributing the actual value?我不应该归因于实际价值吗? Like in the second access.就像在第二次访问中一样。
And what is this number that gives me when i access the pointer?当我访问指针时,这个数字是多少? (First access) (第一次访问)
So, in the first printf, you're not actually accessing the pointer.因此,在第一个 printf 中,您实际上并没有访问指针。 If you have a pointer named myPointer
, writing *myPointer
will actually give you access to the thing that the pointer is pointing to.如果你有一个名为myPointer
的指针,写*myPointer
实际上会让你访问指针指向的东西。 It is understandable to be confused about this, as you do use the *
operator when declaring a variable to specify that it is a pointer.对此感到困惑是可以理解的,因为在声明变量时确实使用*
运算符来指定它是指针。
char* myCharPtr; // Here, the '*' means that myCharPtr is a pointer.
// Here, the '*' means that you are accessing the value that myCharPtr points to.
printf("%c\n", *myCharPtr);
In the second printf, you're accessing the pointer itself.在第二个 printf 中,您正在访问指针本身。 And in the third printf, you're accessing a pointer to a char pointer.在第三个 printf 中,您正在访问指向char 指针的指针。 The &
operator, when put before a variable, will return a pointer to that variable. &
运算符放在变量之前时,将返回指向该变量的指针。 So...所以...
char myChar = 'c';
// pointerToMyChar points to myChar.
char* pointerToMyChar = &myChar;
// pointerToPointerToMyChar points to a pointer that is pointing at myChar.
char** pointerToPointerToMyChar = &pointerToMyChar;
When you're trying to store the value in the array, it's forcing you to do *data
because the array stores chars, and data
is a pointer to a char.当您尝试将值存储在数组中时,它会迫使您执行*data
因为数组存储字符,而data
是指向字符的指针。 So you need to access the char value that the pointer is pointing to.因此,您需要访问指针指向的 char 值。 And you do that via *data
.你通过*data
做到这一点。
Lastly: the reason that printf("1: %d\n", *data);
最后: printf("1: %d\n", *data);
的原因prints a number is that chars are secretly (or not secretly) numbers.打印数字是字符是秘密(或非秘密)数字。 Every character has a corresponding numeric value, behind the scenes.每个字符在幕后都有一个相应的数值。 You can see which numerical value corresponds to which character by looking at an ascii table.您可以通过查看ascii 表来了解哪个数值对应于哪个字符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.