#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int *ptr,a[6]={1,2,3,4,5,6};
int i;
ptr=(int*)calloc(a[6]*sizeof(int),2);
for(i=0;i<7;i++)
{
printf("\n %d",*ptr+a[i]);
}
free(ptr);
getch();
}
程序完美运行!! 但我的问题
calloc() 中 a[6] 的声明是否正确。因为 calloc 的语法是
ptr_var=(cast_type *)calloc(no_of_blocks,size_of_each_block);
.calloc()
有什么作用?和[6]现在就做。因为 ptr 没有分配任何地址,所以 *ptr 的值是多少。
在 printf 中,*ptr 需要在那里。那么它有什么作用。
最佳答案
1.does the declaration of a[6] inside calloc() correct. since the syntax for calloc is
不,它不正确。数组 a
有 6 个元素,索引分别为 0、1、2、3、4、5。但没有 6。
2.what does *ptr takes value because ptr doesnt get assigned any address.
ptr 确实获得了地址。当您调用 calloc() 并将返回地址存储到其中时,您正在为 ptr 分配地址。由于 *ptr 是一个指向 int 的指针,因此它将采用整数作为值。
3.in printf does *ptr needs there.then what does it do.
由于您尚未在 ptr 指向的已分配内存中存储任何值,因此它将全为 0
关于带指针的 calloc() 语法声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26149172/