This code was written specifically for the purpose of identifying a bug in another one of my programs. The first paragraph takes command-line input and encrypts it before printing the original and encrypted text. The second paragraph does the exact same thing, but with the original text hard-coded into it. It works exactly as I would expect it to, and if my command line is the same as the hard-coded text, their outputs are identical.
However, if I simply delete the first paragraph or us /* */ to ignore it, the second paragraph stops functioning properly and starts giving random output as part of the character array. Furthermore, I can determine what this output is by declaring an integer just above the array declaration. for example, adding the line "int a=66;" causes the character array to be followed by the character B (ascii 66) rather than otherwise random characters. I figured that this must have something to do with the way certain bits of memory are stored in character arrays, but I am at a complete loss for why this happens or what to do about it.
Most people would have noticed that and it is almost a good enough reason to not get involved because there appear to be a lot of limitations on successfully running it not least of which are the #include s.
However you need to do a couple of things:
1. Please put <> code tags around your code. Use the toolbox on the right --->
2. Your code should be properly indented and use whitespace to make it legible. I know C programmers like to bunch everything up but that practice died out with the Babbage Difference Engine. :)
3. Also have a function prototype that is hanging in mid air. Delete it please, but there again, you use it in main.
4. 2D array password[i]=argv[1][i]; is very suspect.
They're not huge problems but they make it easier to see what's going on and where people can help you. :)
Sorry, I've only been programming for 2 weeks, and I just know the basics. B function prototype, do you mean the declaration of the crypt() function above main? I took the syntax from the man crypt main page, and I don't think my code works without it being declared before main().
It's OK btunde08 I/we understand. Thanks for the code tags. If I can't help then there are plenty of other people who might jump in.
What I'd like to know about the function
crypt
is the code that goes with it. If you haven't got it or it is very long just a short description of what it does will help. By the look of it it is a process to encrypt a password with a key and it returns it as a c-string.
//The next program shows how to verify a password.
// It prompts the user for a password
//and prints "Access granted." if the user types GNU libc manual.
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <crypt.h>
int main(void)
{
/* Hashed form of "GNU libc manual". */
constchar *const pass = "$1$/iSaq7rB$EoUw5jJPPvAPECNaaWzMK/";
char *result;
int ok;
/* Read in the user's password and encrypt it,
passing the expected password in as the salt. */
result = crypt(getpass("Password:"), pass);
/* Test the result. */
ok = strcmp (result, pass) == 0;
puts(ok ? "Access granted." : "Access denied.");
return ok ? 0 : 1;
}
Yes, the crypt function does do what you just described, but my question is more abstract than just trying to get the right output. I need to understand why a character array (the one called "pass" in my code) which does not call on any variables at all is somehow affected by the code above it (which does not refer to it in any way).
If you cut everything down to the bare minimum, I really want to understand why this code prints "BBB" followed by random characters
1 2 3 4 5
int main(int argc, string argv[])
{
char pass[3]={66,66,66};
printf("%s\n",pass);
}
After playing around a little more, I concluded that it has to do with the input for the main function. By changing main function's input to "void", it starts behaving normally again.
int main(int argc, char* argv[])
{
char pass[3]={66,66,66}; // three characters, not a string
printf("%s\n",pass); // %s expects a c-string
}
1 2 3 4 5 6
int main(int argc, char* argv[])
{
char pass[4]={66,66,66,0}; // three characters, followed by a zero
printf("%s\n",pass); // %s now receives a valid c-string
}
*** Note:this kept going until a zero was encountered each time and the number and value of characters trailing the last B varies each time program runs.
Thank you very much, Chevril. It's good to know that I spent 6 hours scouring and testing code for something that was ultimately fixed with 4 keystrokes. As a side note, do you have any idea why this situation is only a problem with "int main(int argc, char* argv[])"?
If I use "int main(void)" the printf function doesn't seem to care that it isn't a valid c-string.
"As a side note, do you have any idea why this situation is only a problem with "int main(int argc, char* argv[])"?
If I use "int main(void)" the printf function doesn't seem to care that it isn't a valid c-string. "
I don't think you should read any significance into that, it is in my opinion a spurious effect.
Think of it this way. When you put this: char pass[3]={66,66,66}; the compiler has to allocate storage for those three characters. We have no way of knowing what values will reside in the adjacent memory locations. When the code is altered in any way, the contents of those memory locations may change.
You just happened to chance upon some combination where the next memory location contained a zero. But that doesn't mean the error went away. It only meant it was not visibly obvious from looking at the output.
If you look at the examples from Kemort above you will see various ways to address this. Changing the function header isn't one of them ;)