first, when compiling, always enable the warnings, then fix them.
For gcc
, at a minimum use: -Wall -Wextra -Wconversion -pedantic -std=gnu11
Note: other compilers use different options to produce the same result.
Compiling with the warnings enabled results in:
gcc -ggdb -Wall -Wextra -Wconversion -pedantic -std=gnu11 -c "untitled.c" (in directory: /home/richard/Documents/forum)
untitled.c: In function ‘generatePassword’:
untitled.c:12:17: warning: conversion to ‘char’ from ‘int’ may alter its value [-Wconversion]
c = rand() % 23 + 'a';
^~~~
untitled.c:13:28: warning: passing argument 2 of ‘strcpy’ makes pointer from integer without a cast [-Wint-conversion]
strcpy(fullPw, c); // ERROR HERE
^
In file included from untitled.c:2:0:
/usr/include/string.h:121:14: note: expected ‘const char * restrict’ but argument is of type ‘char’
extern char *strcpy (char *__restrict __dest, const char *__restrict __src)
^~~~~~
untitled.c:17:17: warning: conversion to ‘char’ from ‘int’ may alter its value [-Wconversion]
c = rand() % 23 + 'A';
^~~~
untitled.c:19:28: warning: passing argument 2 of ‘strcat’ makes pointer from integer without a cast [-Wint-conversion]
strcat(fullPw, c);
^
In file included from untitled.c:2:0:
/usr/include/string.h:129:14: note: expected ‘const char * restrict’ but argument is of type ‘char’
extern char *strcat (char *__restrict __dest, const char *__restrict __src)
^~~~~~
untitled.c:22:17: warning: conversion to ‘char’ from ‘int’ may alter its value [-Wconversion]
c = rand() % 9 + '1';
^~~~
untitled.c:24:28: warning: passing argument 2 of ‘strcat’ makes pointer from integer without a cast [-Wint-conversion]
strcat(fullPw, c);
^
In file included from untitled.c:2:0:
/usr/include/string.h:129:14: note: expected ‘const char * restrict’ but argument is of type ‘char’
extern char *strcat (char *__restrict __dest, const char *__restrict __src)
^~~~~~
untitled.c:27:17: warning: conversion to ‘char’ from ‘int’ may alter its value [-Wconversion]
c = rand() % 10 + '!';
^~~~
untitled.c:29:28: warning: passing argument 2 of ‘strcat’ makes pointer from integer without a cast [-Wint-conversion]
strcat(fullPw, c);
^
In file included from untitled.c:2:0:
/usr/include/string.h:129:14: note: expected ‘const char * restrict’ but argument is of type ‘char’
extern char *strcat (char *__restrict __dest, const char *__restrict __src)
^~~~~~
untitled.c:33:12: warning: return makes integer from pointer without a cast [-Wint-conversion]
return fullPw;
^~~~~~
untitled.c:33:12: warning: function returns address of local variable [-Wreturn-local-addr]
Compilation finished successfully.
also, the posted code, even after the above problems are fixed will not compile because it is missing the statements:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
Note: just because the list of compiler warnings ends with:
Compilation finished successfully.
Does NOT mean the code is correct. It just means the compiler was able to incorporate some kind of workaround for each of the problems in the code. Usually the result is NOT what you intended
Also, the function: rand()
should be preceded, usually at the top of the main()
function via a call to: srand()
similar to:
#include <time.h>
...
int main( void )
{
...
srand( (unsigned)time( void ) );
to initialize the random number sequence with a random value
strongly suggest reading/understanding the MAN pages for the functions that your program is calling