Home > Net >  pass char to function
pass char to function

Time:08-31

i need to write a function that randomize length of string, allocate dynamic memory for it and than randomize small letters inside it. the main program should print 15 words from the function. the function by itself works for me, i just can't pass it from the main program. my code:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define N 15

char randWord() {
    int len;
    srand(time(NULL));
    len = (rand() % 10)   1;
    char * word = malloc(len);
    
    for (int i = 0; i < len; i  ) {
        word[i] = ((rand() % 26)   97);     
    }

    return word;
}

int main() {
    char* s;
    int i;

    for (i = 0; i < N; i  ) {
        randWord(s);
        printf("%s\n", s);
        free(s);
    }

    return 0;
}

CodePudding user response:

You need to modify your function to accept parameters, and potentially return a char pointer.

char* randWord(char* s)

I think you might have a misunderstanding of functions in general. They can optionally take in parameters, and optionally return something.

A really simple example would be something like this:

int addOne(int x) { 
  return x   1;
}

It may help to think of programming functions in terms of math functions. the above is equivalent to f(x)=x 1. So f(1) "returns" 2, f(2) is 3 and so on.

CodePudding user response:

The function is declared without parameters

char randWord() {

but you are passing a pointer of the type char *.

randWord(s)

The function returns a pointer of the type char * but its return type is char.

char randWord() {
    int len;
    srand(time(NULL));
    len = (rand() % 10)   1;
    char * word = malloc(len);
    
    for (int i = 0; i < len; i  ) {
        word[i] = ((rand() % 26)   97);     
    }
    return word;
}

Also the function does not build a string because the dynamically allocated character array does not contain the terminating zero character '\0'.

And it is a bad style of programming to use magic numbers like 97.

Redefine the function at least like

char * randWord( void ) {
    srand(time(NULL));
    size_t len = (rand() % 10)   1;
    char * word = malloc(len);
    
    if ( word != NULL )
    {
        size_t i = 0;
        for (; i < len - 1; i  ) {
            word[i] = ((rand() % 26)   'a');     
        }
        word[i] = '\0';     
    }

    return word;
}

and in main write

s = randWord();
if ( s != NULL ) puts( s );
free( s );

Though this statement

srand(time(NULL));

should be moved from the function in main before the for loop.

CodePudding user response:

Here is your code annotated and altered to function correctly.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define N 15

char *randWord() { // Return ptr to word buffer (the string)
    int len = rand() % 10   1; // knowing operator precedence avoids overuse of ( )

    char *word = malloc( len   1 ); // allow room for '\0'
    if( word == NULL )
        exit( EXIT_FAILURE ); // do not presume success

    // Here "len" is the # of chars to generate (at least one)
    for( int i = 0; i < len; i   ) {
        word[i] = rand() % 26   'a'; // use ASCII value, not magic number
    }
    word[i] = '\0'; // TERMINATE the string

    return word;
}

int main() { // be consistent with braces
    srand( time( NULL ) ); // Seed the random number generator only once

    for( int i = 0; i < N; i   ) { // Be consistent
        char *s = randWord(); // declare variables proximate to use. Strive for clarity
        puts( s ); // Simpler function than printf
        free( s );
    }

    return 0;
}

It is a subtlety of your code that the returned string will always be at least one character long.

  • Related