Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with functions

Status
Not open for further replies.

WernerSchwarz

Programmer
Jun 30, 2003
11
CH
I want to make a function which can take a textstring as parameter, like this
char string[5]
myfunction(string);

my question, how must the definition of the function look like?
 
Generally, if you want to pass a string of indeterminate length to a function, the definition would look like this:

Code:
void myfunction(char string[]);

Dennis
 
I made it that way, but it didn't work,

void myfunction(char string[]);

int main
{
...
}

void myfunction(char string[])
{
...
}

 
What you specified in your first post should be correct:

Code:
void myfunction(char string[]);

int main
{
...
}

void myfunction(char string[])
{
...
}

What happens when you try to compile/run this?
 
My first post was wrong, correct it should be like this:

void myfunction(char);

int main
{
...
}

void myfunction(char string[])
{
...
}

Functions can't be defined like this: void myfunction(char string[]) they must be like this: void myfunction(char);
 
My compiler will accept the
Code:
void myfunction(char string[]);
definition just fine. What OS and compiler are you using, and what's the exact error message?
 
char string[] will probably give an error because the [] needs a definate value in it. Instead of trying to use char string[] for indefinate array types, why not try using pointers such as char *string ? :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top