hi all,
I am trying to pass an array to a function.

an array is like

char game[4] = {'S', 'R', 'D'};

i wanna pass the entire array to a function which accept.

like this void name(char *game)


How to do this..?

Recommended Answers

All 2 Replies

void name(char game[4])
{
//your void function here
}

void main()
{
char game[4] = {'S', 'R', 'D'};

name(game)

return 0;
}

You actually don't need to use the * because a funny little thing C does by assuming the index "game[0]" (the number in the brackets) is already a pointer when it is called for in a function.

Its a little confusing if its your first time seeing it, but this explains it a little better, http://www.cs.umd.edu/class/spring2003/cmsc311/Notes/BitOp/pointer.html

I prefer to pass arrays like this

void name(char *game, int length)
{
//your void function here
}

int main()
{
char game[4] = {'S', 'R', 'D'};

name(game,4)

return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.