Passing vector to functions

6

How do I pass a vector to a function? Something that, in the Moon, would be like this:

vector = {"V", "e", "t", "o", "r"}
function getVector(vector, pos)
    return vector[pos]
end
print(getVector(vector, 1))
Output: "V"

I tried this way:

#include <stdio.h>
void main() {
    char vector[5] = {"V", "e", "t", "o", "r"};
    char getVector(char vector[], int pos) {
        return vector[pos];
    }
    printf("%c", getVector(vector, 0));
}

The errors:

G:\PROJETOS\C\test.c    In function 'main':
3   5   G:\PROJETOS\C\test.c    [Error] excess elements in char array initializer
3   5   G:\PROJETOS\C\test.c    [Error] (near initialization for 'vector')
3   5   G:\PROJETOS\C\test.c    [Error] excess elements in char array initializer
3   5   G:\PROJETOS\C\test.c    [Error] (near initialization for 'vector')
3   5   G:\PROJETOS\C\test.c    [Error] excess elements in char array initializer
3   5   G:\PROJETOS\C\test.c    [Error] (near initialization for 'vector')
3   5   G:\PROJETOS\C\test.c    [Error] excess elements in char array initializer
3   5   G:\PROJETOS\C\test.c    [Error] (near initialization for 'vector')
    
asked by anonymous 16.12.2014 / 00:35

3 answers

7

The code has some problems:

  • The Main() must return a int . Some compilers accept this differently but it is non-standard, learn to do everything by default.
  • One function can not be inside the other. And the calling function must be declared before its use. The definition of it may be later but it is more practical to do it first (next to the statement, so you do not need to put the header twice) in simple cases like this.
  • You declared the vector as type char but put strings inside it as elements. There is a difference between single and double quotation marks. The character literal is single quotes.

Code:

#include <stdio.h>

char getVector(char vector[], int pos) {
    return vector[pos];
}

int main() {
    char vector[5] = {'V', 'e', 't', 'o', 'r'};
    printf("%c", getVector(vector, 0));
}

See running on ideone . And in Coding Ground . Also I put it in GitHub for future reference .

    
16.12.2014 / 00:48
6

In C you can not pass vectors by value, just by reference (ie you need to pass a pointer to the vector, not the vector itself). However, C's syntax allows you to define your function this way:

char getVector(char vector[], int pos) {

And the compiler ensures that the vector automatically collapses to a pointer. Font .

However, the same is not true for multidimensional vectors:

char vector[5][2] = {"V", "e", "t", "o", "r"};

char getVector(char vector[][], int pos, int pos2) { // Não funciona

char getVector(char vector[][2], int pos, int pos2) { // Funciona

char getVector(char **vector, int pos, int pos2) { // Funciona

Font . Font .

As for your compilation errors:

  • In C there is a difference between a string and a single character: strings are delimited by double quotation marks, and characters in single quotation marks. So your vector initialization should be:

    char vector[5] = {'V', 'e', 't', 'o', 'r'};
    
  • Can not create functions within other functions in C. Pass your getVector out of the function main .

16.12.2014 / 00:48
5

The other answers already solve your problem, but I would like to collaborate.

On Lua there is no difference between "V" , 'V' and [[V]] , as you read here . All treated as strings. But in C single quotation marks and double quotes has different meanings.

In C and C ++, single quotes are used for a single character and the double quotation marks for strings. In the case of strings, the compiler includes a character null or ''%code%'' , to identify the end of the string.

So, this:

char str1[] = "Hello";

Equivalent to this, with %code% at the end:

char str2[] = { 'H','e','l','l','o','
char str1[] = "Hello";
' };

Example: ideone

    
16.12.2014 / 01:35