简体   繁体   中英

size of dynamic array

Hi i want to find out size of my dynamic array

res=scanf(" %d%1s ",&x,c);
            data[cnt++]=x;
                if(x<0){
                printf("Bad input1.\n");
                return 0;
                }
                if(res != 2){
                printf("Bad input2.\n");
                return 0;
                }
                if(cnt >= size);{
                size *=2;
                data=(int*)realloc(data,size*sizeof(int));
                }           
            }
        printf("Length: %d\n",sizeof(data)/sizeof(int));
        }

it keeps telling me size 4 or size 1 even i scanf 5+ numbers ...

You already have the size of the buffer. it's exactly size*sizeof(int) . – kaylum

您使用cnt作为索引添加元素,最后将显示元素计数,只打印它,尽管如果您想要数组长度(而不是添加的元素数量),您已经准备好使用它来分配数组:size * sizeof(int)。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM