Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

C Expert Help


ghost's Avatar
0 0

Is there anyone who really understands pointers well? I've written a program, but its giving me VERY bizarre output, I assume I've messed up my pointer usage. Is there anyone I can pm my source to?


spyware's Avatar
Banned
0 0

Just post it, disable the smilies.


ghost's Avatar
0 0

This is where it goes crazy: brute is called with the length of the string, and the filename to output to. "null" is passed for filename if it isn't being written. Run the code, and you'll see that it really doesn't do what you'd expect.

void brute(int length,char *file){
	if(file!="null"){
		
		out=fopen(file,"w");
	}
	char pass[length];
	int x;
	int y;
	printf("Brute.Length= %d\n",length);
	printf("Brute.File=%s\n",file);
	while(done(pass)==0){
		for(x=0;x<length;x++){
			y=pass[x];
			if(y==0){
				pass[x]=48;
			}
			if(y>122 && x!=length){
				pass[x]=48;
				pass[x+1]++;
			}
			pass[x]++;
		}
		printf("Key: %s\n",pass);
		if(file!="null")
			fprintf(out,"Key: %s\n",pass);
		
	}
	if(file!="null")
		fclose(out);
}

ghost's Avatar
0 0

well first off, in your test to determine if the pointer is null, "null" shouldn't be in quotes. for example:

markupif (file != NULL) {

or even

markupif (!file) {

should work.

that's the only thing I can see…I haven't coded in C in a while though.


ghost's Avatar
0 0

Yes, but as i said, "null" is passed to brute if no file is needed. So I am checking against a string. Thanks for trying though.


ghost's Avatar
0 0

oh I get it…hehe.

in that case you should use strcmp(), which takes 2 strings and returns 0 if they're the same.

edit: for example

markupif (strcmp(file, "null") == 0) {

should work.


ghost's Avatar
0 0

The file writting is fine. What ends up happening is that when it prints key, it appends a bunch of random characters.

Thanks for helping me on this.


ghost's Avatar
0 0

Try to initialize the string or memset it:

//this will fille pass with 0s
memset(pass,0,length)

Just my quick 'n'dirty two cents


ghost's Avatar
0 0

It looked promising, but it didn't work. Thanks anyway.


ghost's Avatar
0 0

it might help if you posted the done() function, other than that I've got nothing…


ghost's Avatar
0 0

Im sure that the done function works fine. I tested it thoroughly, and built a test app off it. So I guess no one knows what the issue is then?


ynori7's Avatar
Future Emperor of Earth
0 0

instead of passing in a pointer for file, maybe try passing in a copy of the variable.


ghost's Avatar
0 0

But file output works fine. Maybe my issue isn't because if pointers…


ghost's Avatar
0 0

The error is in the algorithm, each cycle increases all chars in pass. pass = 000, 111, 222, … It also overwrites the null terminator, giving gibberish in the output.

This code gives proper output:

#define LOW 48
#define HIGH 122

void brute(int length, char *file) {
    char pass[length+1];
    int x, f;
    FILE* out;

    f = !strcmp(file, "null");

    if(f)
        out=fopen(file,"w");

    for(x=0;x<length;x++)
        pass[x]=LOW;
    pass[length]=0;

    printf("Brute.Length= %d\n",length);
    printf("Brute.File= %s\n",file);

    while(done(pass)==0) {
        printf("Key: %s\n",pass);
        if(f)
            fprintf(out,"Key: %s\n",pass);

        for(x=0;x<length;x++) {
            if(pass[x]++<=HIGH)
                break;
	
            pass[x]=LOW;
        }
        if(x==length)
            break;
    }

    if(f)
        fclose(out);
}