Luke Collins
Luke Collins

Reputation: 1463

How do I pass an array as an argument to a function?

I have read the post here on the same subject, but it doesn't seem to have the solution to my problem.

If I need to write a function str_reverse() which reverses any string passed to it, how can I go about it? Just as the person who asked the question I linked above, the following code

#include <stdio.h>
#include <string.h> //for strlen

#define maxL 300 //Max String Length

void str_reverse(char);

int main(){

    //Variables
    char x[maxL];

    //User Prompt
    printf("Enter a string no longer than %d characters: ", maxL);
    gets(x);
    str_reverse(x);

    //Return Statement
    return 0;
}

void str_reverse(char x){
    int i, l;
    l = strlen(x);
        printf("In reverse: ");
        for(i=l-1; i>=0; i--)
            printf("%c",x[i]);
}

gives me an error. How can I create a program which allows me to reverse the string?

Upvotes: 0

Views: 204

Answers (4)

Vlad from Moscow
Vlad from Moscow

Reputation: 310950

A function parameter declared as an array is adjusted to pointer to array element.

Thus these function declarations are equivalent and declare the same one function

#define maxL 300 
void str_reverse(char[maxL]);

or

void str_reverse(char[10]);

or

void str_reverse(char[1000]);

or

void str_reverse(char[]);

or

void str_reverse( char *);

And on the other hand when an array is passed as an argument to a function it is implicitly converted to pointer to its first element.

So this function call

char x[maxL];
//...
str_reverse(x);

can be imagine like

char x[maxL];
//...
char *tmp = x;
str_reverse(tmp);

Take into account that function gets is unsafe and is not supported by the C Standard any more.

Use instead standard function fgets

Also it is better if the function returned pointer to its first character.

Your function does not try to reverse a string. It tries to output a string in the reverse order.

So the function that indeed reverses a string can be written like

char * str_reverse( char *s )
{
    size_t n = strlen( s );

    for ( size_t i = 0; i < n / 2; i++ )
    {
        char c = s[i];
        s[i] = s[n- i-1];
        s[n-i-1] = c;
    }  

    return s;
}

Here is a demonstrative program

#include <stdio.h>
#include <string.h>

#define maxL 300 //Max String Length

char * str_reverse( char *s )
{
    size_t n = strlen( s );

    for ( size_t i = 0; i < n / 2; i++ )
    {
        char c = s[i];
        s[i] = s[n- i-1];
        s[n-i-1] = c;
    }  

    return s;
}

int main( void ) 
{
    //Variables
    char s[maxL];

    //User Prompt
    printf("Enter a string no longer than %zu characters: ", maxL);
    fgets( s, maxL, stdin );
    s[strcspn( s, "\n" )] = '\0';

    puts( str_reverse( s ) );

    return 0;
}

Its output might look like

Enter a string no longer than 300 characters: Hello, World!
!dlroW ,olleH

Upvotes: 1

CodeWarrior101
CodeWarrior101

Reputation: 123

Your can pass array as type arr[] or as type * pointer. you will have to pass length as an argument to your string reverse function. New declaration of string reverse will look like this:

void str_reverse(char *x,int length)

or

void str_reverse(char x[],int length)

for more information you can follow Length of array in function argument

Upvotes: 1

You can achieve this by many different ways:

Option-1 Formal parameters as a pointer −

void myFunction(char *param) {
   .
   .
   .
}

Option-2 Formal parameters as a sized array −

void myFunction(char param[10]) {
   .
   .
   .
}

Option-3 Formal parameters as an unsized array −

void myFunction(char param[]) {
   .
   .
   .
}

Upvotes: 0

P.P
P.P

Reputation: 121387

You are passing fine but the function isn't receiving properly. Change it to:

void str_reverse(char *x){
..
}

and the prototype to:

void str_reverse(char*);

Also, gets() is dangerous and should never be used. Use fgets() instead:

if( fgets(x, sizeof x, stdin) == NULL) { /* failure */ }

x[strcspn(x,"\n")] = 0; // to remove the trailing newline, if any.

Upvotes: 3

Related Questions