-4

First of all I know that array A degrades to pointer when we call function f(int a[]) and f(int *p) is same.

BUT:

1.I really need sending by value all array.
2.I really need that sending size is non const in function (but const size in plase we calling function)

I write some example:

http://ideone.com/ZbW0wT

#include <stdio.h>
#define SZ 15
typedef struct {int a[SZ];} rec;
int main(){
        void pa(rec);
        int value[SZ] ={9,8,7,6,5,4,3,2,1,0};
        pa(*(rec*)value);
        printf("%u %u\n",sizeof(rec),sizeof(value));
        return 0;
}
void
pa(rec b){
        int z;
        for(z=0;z<SZ;z++){
            printf("array[%2d] is %d\n",z,b.a[z]);
        }
}

This code work for const size , but how change so pa would get by value some rec which size depend on passed array?

Update: it must by value sended , but not const sized as in Pascal etc , but in true C way , all pass by value not by pointer on 0 element

and function need universal so user can write func(variablesizeArrayOfT) where arg passed by value.

if possible need standard way (C11 or better C99 or better C89 or better K&R), if cant then gcc

UPD2: http://ideone.com/H4XGqC #include

typedef struct{
        int     len;
        int     a[];
} av;

void f(av a){
        while(a.len--){
            printf("array[%2d] is %d\n",a.len,a.a[a.len]);
        }
}

int main(){
        int b[]={3,1,2,3};
        int c[]={7,1,2,3,4,5,6,7};
        f(*(av*)b);
        f(*(av*)c);
        return 0;
}

all good by probably bug in alignment so size(3 and 7) is right but value of a[] is not

UPD3 see throw gcc -g -c 2ndSRC.c &&objdump -d -M intel -S 2ndSRC.o

it just send only size (b[0] and c[0]) but not all array

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
qulinxao
  • 183
  • 1
  • 10
  • 1
    Tip: Always compile with full warnings, and *handle them all*: `-Wall -Wextra -pedantic`. – Deduplicator Jul 06 '14 at 15:52
  • 1. Get rid of the struct, use a simple array. 2. Declare and implement function `void pa(int arr[],int size)`. Note that it doesn't return anything, hence the return type is `void`. 3. From `main`, call `pa(value,sizeof(value)/sizeof(*value))`. 4. Declare function `pa` **outside and before** function `main`!!! – barak manos Jul 06 '14 at 15:55
  • Deduplicator's suggestion assumes `gcc` (which is usually a pretty good assumption). `-Wall` is by far the most important flag of those three; the others are nowhere near as useful. You may also want to add `-std=c99` to choose the C99 standard. – ooga Jul 06 '14 at 15:59
  • @ooga: What's wrong with `-pedantic` if you don't want to rely on gcc extensions? – mafso Jul 06 '14 at 16:06
  • With variable size array do you mean a dynamically allocated array? – Étienne Jul 06 '14 at 16:27
  • no. variable size i mean i have int b[3], a[7] as local or global array and function func(some args, and argsArrayByvalue) . so that func can work as on b[3] as on a[7] – qulinxao Jul 06 '14 at 16:33
  • @mafso I didn't say there was anything wrong with it (although it's an assumption to think he doesn't want to rely on the very useful gnu extensions). I just said that the most important one was `-Wall`. – ooga Jul 06 '14 at 16:46
  • @qulinxao In C you it is not possible to "pass an array by value". What is the problem with passing a pointer to the first member of the array? You just have to pass the size of the array as second parameter. – Étienne Jul 06 '14 at 17:05
  • @Étienne as u can see in 1st src we can pass array by value in C if we can say size of it on compile time so Question . how write universal func on array of int where array of int passed by value . – qulinxao Jul 06 '14 at 17:13
  • @qulinxao In your first test you are passing a structure by value, not an array directly. See also this question about getting the size of an array (passed to a function): http://stackoverflow.com/questions/37538/how-do-i-determine-the-size-of-my-array-in-c – Étienne Jul 06 '14 at 17:46
  • Actually someone already answered one of your questions by telling you it is not possible to pass an array by value in C: http://stackoverflow.com/questions/21969095/how-to-send-array-by-value-to-function-in-c – Étienne Jul 06 '14 at 17:52
  • pls @Étienne see throw gcc what code generate this: pa(\*(rec\*)value); it copy sizeof(rec) byte from value to stack my only problem that sizef of copy not in runtime but compile time so if i can copy more than needed byte big rec is aproptiate solution my current question is how copy as litle is pocible but get data by value, not by reference – qulinxao Jul 06 '14 at 17:57

1 Answers1

0

An idiomatic way to have arrays containing data of variable length in C is to use a buffer with a maximum size which is known at compile time, is that what you wanted?

#include <stdio.h>

#define MAX_SIZE 15
typedef struct {
    int arr[MAX_SIZE];
    size_t arr_len;
} rec_t;

void pa(rec_t rec){
    for(int z=0; z<rec.arr_len; z++){
        printf("array[%2d] is %d\n", z, rec.arr[z]);
    }

}

int main(void){
        rec_t rec ={
            .arr = {9,8,7,6,5,4,3,2,1,0},
            .arr_len = 10
        };
        pa(rec);
}
Étienne
  • 4,773
  • 2
  • 33
  • 58
  • I find (*(rec)arraypointer) it get me sizeof rec chunk from *arraypointer. But i dont know how get variable size chunk by value . – qulinxao Jul 06 '14 at 16:20