views:

82

answers:

3

so what I'm trying to accomplish is generating 100 random 0's and 1's add them all into one variable and then print it. What I have right now I don't know how to make work. If someone could explain what I'm doing wrong, I would be very grateful.

randstring (void){
    int i;
    int num;
    char buffer[101];
    i=100;
    while(i>0, i--){
     num = rand()%2;
     strcpy(buffer, num);
    }
    return(buffer);
}

so what i have now is:

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

main (void){
    printf("%f", randstring());
}
randstring (void){
    int num;
    char buffer[101];
    int i = 100;
    while(i-- >= 0) buffer[i] = rand() % 2;
    return(buffer);
}
A: 
/* Returns a malloc'ed buffer; don't forget to free it once you 
   have finished using it */
char * randstring (int len)
{    
 int i;   
 char * buffer = malloc(len + 1);   

 i = len;    
 while (i-- > 0)
 {        
    buffer[i] = '0' + (char) (rand() % 2);           
 } 

 buffer[len] = '\0';

 return (buffer);
}
Mitch Wheat
rand() returns int.
alamar
Also, (char) 1 wouldn't actually work.
alamar
Thanks, updated...
Mitch Wheat
That wouldn't work either.
alamar
everytime I eidted I got the cahed previoius changes. Code should now work!
Mitch Wheat
Yeah, but I'd recommend s/48/'0'/ for clarity.
alamar
good point! ...
Mitch Wheat
BTW, does anyone else have cahching issues with editing? Since I started using IE8 I get weird cachced page problems
Mitch Wheat
Nope; then again, nobody use IE8...
alamar
+6  A: 

How about buffer[i] = (rand() % 2) ? '1' : '0'; in the loop body?

And I'd do buffer[100] = 0;

But the worse problem is that you can't return buffer, because as soon your function exits, it would be overwritten. It is allocated on the stack, and the stack gets reused when function exits. You need to either do malloc and free, or pass buffer and its length to this function.

Which gives us:

#include <stdio.h>

#define RAND_LENGTH 100

char *randstring (char *buffer, int length);

int main (int a, char **b){
    char buffer[RAND_LENGTH + 1];
    printf("%s", randstring(buffer, RAND_LENGTH));
}

char *randstring (char *buffer, int length){
    int i = length;
    while(--i >= 0) {
        buffer[i] = (rand() % 2) ? '1' : '0';
    }
    buffer[length] = 0;
    return buffer;
}
alamar
+1. From me. Might be worth updatinbg to show complete code?...
Mitch Wheat
even better. nice one!
Mitch Wheat
Another minor correction.
alamar
wow, thanks. as you can tell im just learning here so thanks for explaining it a bit, now to decode what you did and learn :).
austin
Read the scattered comments, I hope I made it clear.
alamar
+1  A: 

Try this:

int i = 100;

while(i-- >= 0) buffer[i] = rand() % 2;
Shakedown
This is not good: you fill character buffer with 0 and 1. It will be not printable. What he probably wanted is '0' and '1'.
Igor Krivokon
this is working well but the return gives me the error ofmain.c:35: warning: return makes integer from pointer without a castmain.c:35: warning: function returns address of local variableall done
austin