C中的指针(应该很简单)

时间:2011-06-08 05:53:09

标签: c pointers

我尝试了这样的代码:

int *a;
*a = 10;
printf("%d",*a);
在eclipse中

并没有打印出任何东西。 是因为我没有给一个初始值?


谢谢你的帮助。我知道这是有问题的我只是不确定确切的问题 比如,如果我做printf("%d",a);我可以看到它确实包含某些东西,这是C的规则 我必须给它一个指向的地方然后我可以开始更改该地址中的值吗?

3 个答案:

答案 0 :(得分:15)

  • int *a;这定义了一个变量,它是一个指向整数类型的指针。创建时的指针类型变量a包含垃圾值。
  • 执行*a = 10;时,它会将a中存储的值(垃圾)用作地址,并将值10存储在那里。因为我们不知道a包含什么并且没有分配,所以a指向一些未知的内存位置并且访问它将是非法的,并且会给你一个分段错误(或类似的东西)
  • printf ("%d", *a);的情况相同。这也会尝试访问存储在未分配的未定义内存位置的值。

this variable is       this is the location
stored in some         with the address 
address on the         'garbage'. You do not
stack                  have permissions to
                       access this
+-------+---------+
| name  | value   |
+-------+---------+     +---------+
|  a    | garbage |---->| ?????   |
+-------+---------+     +---------+

定义指针类型变量后,需要从操作系统请求一些内存位置,并使用该内存位置值存储到a,然后通过{{1}使用该内存位置}。

为此,您需要执行以下操作:

a

在这种情况下,如下所示:

int *a; a = malloc (sizeof (int)); /* allocates a block of memory * of size of one integer */ *a = 10; printf ("%d", *a); free (a); /* You need to free it after you have used the memory * location back to the OS yourself. */ 之后。指针变量在堆栈中分配。此时*a = 10;包含垃圾值。然后a指向具有该垃圾值的地址。

a

this variable is this is the location stored in some with the address address on the 'garbage'. You do not stack have permissions to access this +-------+---------+ | name | value | +-------+---------+ +---------+ | a | garbage |---->| ????? | +-------+---------+ +---------+ 之后。我们假设a = (int *) malloc (sizeof (int));会返回您要使用的地址malloc。此时0x1234abcd将包含a,然后0x1234abcd指向有效的内存位置,该位置已分配并保留供您使用。但请注意a内的值可以是任何值,即。垃圾。您可以使用0x1234abcd设置分配给calloc的内存位置的内容。

0

this variable is this is the location stored in some 0x1234abcd , allocated address on the by malloc, and reserved stack for your program. You have access to this location. +-------+------------+ | name | value | +-------+------------+ +---------+ | a | 0x1234abcd |---->| garbage| +-------+------------+ +---------+ 之后,*a = 10;访问内存位置*a并将0x1234abcd存储到其中。

10

this variable is this is the location stored in some 0x1234abcd , allocated address on the by malloc, and reserved stack for your program. You have access to this location. +-------+------------+ | name | value | +-------+------------+ +---------+ | a | 0x1234abcd |---->| 10 | +-------+------------+ +---------+ 之后,free (a)的内容即。内存地址a将被释放,即返回给操作系统。请注意,在释放0x1234abcd之后0x1234abcd的内容 a,但您无法再合法访问它,因为您刚刚释放了它。访问存储在0x1234abcd中的地址所指向的内容将导致未定义的行为,很可能是分段错误或堆损坏,因为它已被释放且您没有访问权限。

a

<强> EDIT1

另请注意this variable is this is the location stored in some 0x1234abcd , allocated address on the by malloc. You have freed it. stack Now you CANNOT access it legally +-------+------------+ | name | value | +-------+------------+ +---------+ | a | 0x1234abcd | | 10 | +-------+------------+ +---------+ the contents of a remains the same. printf ("%d", a);之间的区别。当您引用printf ("%d", *a);时,它只会打印a a的内容。当您引用0x1234abcd然后它使用*a作为地址,然后打印地址的内容,在这种情况下为0x1234abcd

10

<强> EDIT2

另请注意,this variable is this is the location stored in some 0x1234abcd , allocated address on the by malloc, and reserved stack for your program. You have access to this location. +-------+------------+ | name | value | +-------+------------+ +---------+ | a | 0x1234abcd |---->| 10 | +-------+------------+ +---------+ ^ ^ | | | | (contents of 'a') (contents of the ) | (location, pointed ) printf ("%d", a); ( by 'a' ) | +----------------+ | printf ("%d", *a); 无法为您提供有效的内存位置。您应该始终检查malloc是否返回了有效的内存位置。如果malloc无法为您提供一些内存位置,那么它会返回malloc,因此您应该在使用前检查返回值是否为NULL。所以最后代码变成了:

NULL

答案 1 :(得分:10)

你没有为a分配内存。

尝试

int *a;
a = (int*)malloc(sizeof(int)); //Allocating memory for one int.

*a = 10;
printf("%d", *a);
free(a); //Don't forget to free it.

答案 2 :(得分:1)

声明指针时,编译器只会保留内存来存储指针变量。如果你希望指针指向事后的东西,它必须是分配了自己的内存的东西。要么将它指向一个被声明为int的东西,要么从堆中为int分配内存。