将指针数组传递给函数和计数大小

时间:2014-02-22 19:50:00

标签: c arrays pointers

我有以下代码,我试图在函数中传递数组,所以我可以扩展数组。但我坚持如何获得数组的长度。

我也试过很多这个代码。请指导

编辑如何将poniter数组的大小从默认值4更改为size+1,我尝试了malloc但是没有正确理解它并且没有用。< / p>

#include <stdio.h>
#include <stdlib.h> 
#include <conio.h>

struct Item {
    int item;
    double value;
};

void addItem(Item *arrPtr, int &size, int &count, int item, double value)
{
    ++size;
    arrPtr = (Item *)malloc(size*sizeof(arrPtr));
    arrPtr[count++].item = item;
    arrPtr[count++].value = value;
}

int main(void){

    int size = 0;
    int count = 0;
    Item arrPtr[size];

    addItem(arrPtr, size, count, 1, 239.99);
    addItem(arrPtr, size, count, 2, 129.75);
    addItem(arrPtr, size, count, 3, 99.95);
    addItem(arrPtr, size, count, 4, 350.89);    

    for(int i = 0; i < size; i++)
    {
        printf("%d, %f \n", arrPtr[i].item, arrPtr[i].value);
        printf("%d, %f", arrPtr[i].item, arrPtr[i].value);
    }

    //printf("%d, %f \n", arrPtr[0].item, arrPtr[0].value);
    //printf("%d, %f", arrPtr[1].item, arrPtr[1].value);
    getch();
    return 0;
}

3 个答案:

答案 0 :(得分:1)

C指针不记录指向的数组的长度。显式传递大小,或者建立一个约定,其中特定值标记数组的逻辑结尾(这就是C字符串以空字符结尾的原因),或构建一个包含数组指针和长度字段的结构并传递周围。

答案 1 :(得分:0)

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

typedef struct ItemLabel {  // made it a typedef (type definition)
    int item;               // replaced Item with ItemLabel
    double value;
} Item;                     // called my new type Item

void addItem( Item ** arrPtr, int * size, int * count, int item, double value )
// Receiving the pointers for size and count, to achieve what you want to achieve

{
    ++( *size );

    // getting the size of the element, instead of the pointer...
    // using realloc instead to expand the size, as you seem to want
    *arrPtr = realloc( *arrPtr, ( * size ) * sizeof( ** arrPtr ) );
    // also no typecasting voidPtr

    (*arrPtr)[*count].item = item;
    (*arrPtr)[*count].value = value;
    ( *count )++;
    // incrementing count twice, would increment the count twice
    // I don't think you would want that...
}

int main( ){    // removed the void argument

    int size = 0;
    int count = 0;
    Item * arrPtr = NULL;

    // sending addresses of size & count, so that they will actually get increased
    // also the arrPtr, so that the pointer will also get actually modified
    addItem( &arrPtr, &size, &count, 1, 239.99 );
    addItem( &arrPtr, &size, &count, 2, 129.75 );
    addItem( &arrPtr, &size, &count, 3, 99.95 );
    addItem( &arrPtr, &size, &count, 4, 350.89 );

    for ( int i = 0; i < size; i++ )
    {
        printf( "%d, %f\n", arrPtr[i].item, arrPtr[i].value );
        // removed the second printf, put it back if you want...
    }

    //printf("%d, %f \n", arrPtr[0].item, arrPtr[0].value);
    //printf("%d, %f", arrPtr[1].item, arrPtr[1].value);

    // your function ends anyway, but it's never bad to free
    free( arrPtr );
    getch( );
    return 0;
}

您可以在上面的代码中找到一堆评论。它做了我认为你希望它在我的目的做的事情。确切地说,这实际上并不适用于我,因为我在Windows上使用MSVC,所以I cannot use getch( );;为了使其工作,我只需将其替换为_getch( );

更改主要涉及发送您想要 真正 更改的变量的地址,以及addItem函数的参数列表{{1} }第

*部分也很重要,它允许您使用简单的typedef来引用类型似乎想要;否则你必须把它称为Item,长篇大论......

我没有更改它,但我建议只使用struct ItemLabel {blabla},而不是sizesize {{1}这个函数,只是因为它们几乎总是带有相同的值。在这种情况下,您必须更改countint item作业,如下所示:

.item

随意提出任何进一步的混淆......

答案 2 :(得分:0)

Item arrPtr[size];

这个可变长度数组,除非编译C99或更高版本,否则不能使用它。

arrPtr = (Item *)malloc(size*sizeof(arrPtr));

在将数组作为指针传递时,即使将此设置为指针,也不会更改原始文件,因为复制它。

从以上方面做以下几种方式。

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

typedef struct Item {
    int item;
    double value;
} Item; //need typedef in C (not C++)

Item *addItem(Item *arrPtr, int *size, int *count, int item, double value){
    ++*size;
    arrPtr = (Item *)realloc(arrPtr, (*size)*sizeof(*arrPtr));//sizeof(Item), not sizeof(Item*)
    arrPtr[*count].item    = item;//count : The count too if you count up here
    arrPtr[(*count)++].value = value;
    return arrPtr;//return a pointer that has changed
}

int main(void){
    Item *arrPtr=NULL;//use it as a pointer
    int size  = 0;//Capacity
    int count = 0;//The number of data that is actually stored
    int i;

    //&size, &count : Can not be used reference in C(not C++, You are using the C++ probably)
    arrPtr = addItem(arrPtr, &size, &count, 1, 239.99);
    arrPtr = addItem(arrPtr, &size, &count, 2, 129.75);
    arrPtr = addItem(arrPtr, &size, &count, 3, 99.95);
    arrPtr = addItem(arrPtr, &size, &count, 4, 350.89);    

    for(i = 0; i < count; i++){//int i : C99 syntax
        printf("%d, %f \n", arrPtr[i].item, arrPtr[i].value);
//      printf("%d, %f", arrPtr[i].item, arrPtr[i].value);//Duplicate
    }
    free(arrPtr);//release
    getch();//not portable
    return 0;
}