带指针的 calloc() 语法声明

标签 c pointers

#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();
}

程序完美运行!! 但我的问题

  1. calloc() 中 a[6] 的声明是否正确。因为 calloc 的语法是 ptr_var=(cast_type *)calloc(no_of_blocks,size_of_each_block); .calloc()有什么作用?和[6]现在就做。

  2. 因为 ptr 没有分配任何地址,所以 *ptr 的值是多少。

  3. 在 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/

相关文章:

c - 在c中使用指针和函数显示二维数组

c - 为什么复合赋值或迭代运算符不能处理取消引用的指针

c - C语言如何将字符串插入到链表中?

c++ - 使用结构本身的类型定义结构参数

c - 如果++a返回左值那么为什么&(++a)显示编译错误?

c - 并行化变量声明是否有益?

c - shell 如何处理重定向

c - 什么是完全提升类型?

c - 释放 char 指针会将其从我保存的位置删除

c - 需要帮助将文件内容扫描到指针数组中