0

I wonder how can I tease out an array in C to several arguments of a function. After I saw the amazing syntatic sugar from Go (golang) I thinking about it.

The c code:

#include <stdio.h>
#include <stdarg.h>

// assert: all args are valid ints
void printEach(int len, ...) {
        // Irrelevant, this function print their arguments
        // And I know how to use va_start, va_arg...
}

void handleVet(int* v, int n) {
   // **HERE is my the question!!!**
   printEach(n, v[0]...[n]) // <----------- NON-C code. I need it.
}

int main(void) {
    int v[] = {12,14,15,15};
    //I can do that only because the vector is static. I know the len when I'm coding
        printEach(4, v[0],v[1],v[2],v[3]);
    // But if we imagine an arbitrary vector, we got a problem 
       handleVet(v, 4);
    return 0;
}

By Example, in go it would be:

package main

import "fmt"

func printEach (v ...int64) {
    // Irrelevant, this function print their arguments
}

func main() {
    var arr []int64 = []int64{1,14,15,}
    printEach(arr...)
}

How can I achieve the same effect of "printEach(arr...)" in C?

Danilo
  • 93
  • 11
  • If you want a solution in C don't tag C++. – Borgleader Dec 18 '14 at 13:16
  • Must it definitely be an **array**? – Nard Dec 18 '14 at 13:16
  • I don't know go. But is he asking for var_arg in C or is this more simple? – dhein Dec 18 '14 at 13:26
  • @Zaibis yes,I'm talking about "var_arg". I've edited my question to make it easier to understand. The problem is: imagine a generic scenario, when I have the array length only in runtime. not in "coding time". Thanks! – Danilo Dec 18 '14 at 13:37
  • @Danilo Okay here's what I think you **need** to do. 1. Provide a use case in C of how you want the function to be used. 2. Show what you've tried so far that didn't work so we know where you're heading. – Nard Dec 18 '14 at 14:13
  • @Nard, Now, I realize that this question is a "theoric question about programming" and it should be posted at "programmers.stackexchange". There are use cases for that concept, but not to in C, because " the way of C" is different from "the way of Go" I'm trying emulate the GO way in C just out curiosity. – Danilo Dec 18 '14 at 14:27

3 Answers3

0

You will need to specify the size of the array. Here's what it might look like:

void printEach(int* values, int size)
{
  if(size==0)
    return;
  printf("%d", value[0]);
  printEach(values+1, size-1);
}
atoMerz
  • 7,534
  • 16
  • 61
  • 101
0

This is a rudimentary example on how vararg is working in C. I wasn't able to take refference to your go example as I don't udnerstand what your code does. I hope this minimal example is clear enough. If you have any questions ask me and I will edit it in.

void Foo(size_t sizeParamAmount, char *types, ...);
void Foo(size_t sizeParamAmount, char *types, ...)
{
    size_t i;
    float fTmp;
    int iTmp;    
    va_list vlArgList;

    va_start (vlArgList, sizeParamAmount);

    for (i= 0; i< sizeParamAmount; i++)
    {
        switch (types[i])
        {           
            case 'i':
                iTmp = va_arg (vlArgList, int));
                break;
            case 'f':
                fTmp = va_arg (vlArgList, float));
                break;
            default:
                return;//error
        }
    }
    va_end(vlArgList);
}

After reading your edit:

As I already did in my minimal example, you can hand in a pointer before the var_arg's which is explaining which argument is of what type. so you could call Foo this way:

Foo (3, "ifi", 3, 5.973, 92);

And after reading your comment to another answer I got what you are asking about.

In that case you really jsut should hand in a pointer (or array without [] behaves for this case the same) which holds an end content token.

Anyway there is a way. but you had to freak around with preprocessing tokens.

And would be totally over the top for your needs. This answer would anyway give you the requested notation. you had to set for PRED a limit by sizeof(yourarray) and the let OP take the single elements.

https://stackoverflow.com/a/10542793/2003898

But there is sadly not a more minimal example.

Community
  • 1
  • 1
dhein
  • 6,431
  • 4
  • 42
  • 74
  • It still is not what I want. I'll try explain it again in my question. But nice answer (I cannot give a up vote to you because my reputation is low. I'm sorry). – Danilo Dec 18 '14 at 14:05
  • 1
    Ok. I choice your answer, but I think you gave so many focus on the wrong answer. I knew how to use stdarg.h in C. The most important part of your answer is the final. – Danilo Dec 19 '14 at 02:07
0

You are looking for Variadic function, you should look at stdarg.h and varargs.h

dvhh
  • 4,724
  • 27
  • 33