“sizeof的”不完全类型的无效的应用程序“INT []”访问时整数数组由指针指向 [英] invalid application of 'sizeof' to incomplete type 'int[]' When accessing integer array pointed by a pointer

查看:90
本文介绍了“sizeof的”不完全类型的无效的应用程序“INT []”访问时整数数组由指针指向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想学习使用C的指针和写这一点整数数组的指针运动,但跑进的sizeof的 A无效的应用程序不完全型 INT [] 问题。请告诉我为什么我有什么错,如何解决它。谢谢你。

I'm trying to learn pointer in C and am writing this little integer array pointer exercise,but ran into a invalid application of sizeof to incomplete type int[] problem. Please tell me where did I go wrong and how to solve it. Thank you.

#include <stdio.h>

int intA[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int intB[];

void int_copy(const int *source, int *destionation, int nbr)
{
    int i;
    for(i=0;i<nbr;i++)
    {
        *destionation++ = *source++;
    }
}

int main()
{
    int *ptrA = intA;
    int *ptrB = intB;

    int sizeA = sizeof(intA);
    int nbrA = sizeof(intA)/sizeof(int);
    printf("\n\n");
    printf("[Debug]The size of intA is:%d\n", sizeA);
    printf("[Debug]That means the number of elements is:%d\n", nbrA);

    printf("\n\nThe values of intA are:\n");
    int i;
    for(i=0;i<nbrA;i++)
    {
        printf("[%d]->%d\n", i, intA[i]);
    }


    int_copy(ptrA, ptrB, nbrA);
    int sizeB = sizeof(intB);
    int nbrB = sizeof(intB)/sizeof(int);
    printf("\n\n");
    printf("[Debug]The size of intB is:%d\n", sizeB);
    printf("[Debug]That means the number of elements is:%d\n", nbrB);

    printf("\n\nThe values of intB are:\n");
    for(i=0;i<nbrB;i++)
    {
         printf("[%d]->%d\n", i, *ptrB++);
    }

}

# cc -g -o int_copy int_copy.c
int_copy.c: In function 'main':
int_copy.c:36: error: invalid application of 'sizeof' to incomplete type 'int[]' 
int_copy.c:37: error: invalid application of 'sizeof' to incomplete type 'int[]'

这是我观察到的奇怪的是,当我跑GDB,我监视的复制功能,int_copy,运行于这似乎是正确的9倍,但INTB的复制功能后,只打印数组中显示一个项目。

The strange thing that I observed is when I ran gdb, I monitored that the copy function, int_copy, runs for 9 times which seems to be right, but the print of intB after the copy function only displays one item in that array.

我仍然在努力指点一下了,所以请不要帮我,原谅我的无知。非常感谢你。

I'm still struggling about pointers now, so please do help me and forgive my ignorance. Thank you very much.

推荐答案

INTB 基本上是一个指针,而的sizeof 它会产生相同的的sizeof INT ,这就是为什么打印只出现一次。
INTA 是一个已知大小的数组,所以的sizeof

intB is basically a pointer, and sizeof on it will yield the same as sizeof on int, that's why the print appears only once. intA is an array with a known size, so the sizeof works.

您需要记住,的sizeof 不是一个运行时调用,虽然它可能看起来那么语法。这是一个内置的操作,在编译时返回类型的大小以字节为单位,并在编译时间 INTB 是一个指针应该稍后到一个新分配数组。

You need to remember that sizeof is not a run-time call, although it may look so syntactically. It's a built-in operator that returns the size of the type in bytes at the compilation time, and at the compilation time intB is a pointer that should later point to a newly allocated array.

这篇关于“sizeof的”不完全类型的无效的应用程序“INT []”访问时整数数组由指针指向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆