February 13th, 2013, 03:50 PM
String in C
I know there's really no strings in C. But I need some help if you may:
char password(char pass);
char password(char pass)
May I ask, if I want to return pass what data type do I need? If I use char it errors.
February 13th, 2013, 04:07 PM
string in C is really a char array.
You can see the function accept char, so it should return the same.
In standard C, i think/guess that char* would be more correct.
If you work with strings I suggest you look up how pointers work, if you haven't already.
February 13th, 2013, 04:48 PM
Yeah. You're right. I had to go more-in-depth about pointers.
I read in my book on C that the variable also takes a value as a pointer.
char pass(char msg);
char pass(char msg)
Why isn't this working? Keep in mind, my main scenerio I changed was *msg
February 13th, 2013, 04:58 PM
OK, array names and pointers are mostly equivalent -- there are a few things that you can do with pointers that you can't do with arrays. So there shouldn't be any difference between pass(char msg) and pass(char *msg).
Where you're messing up is in the return type. If you want to return a char array, you're actually returning a char pointer. The way you have pass written now, you're only returning the first char of msg, which then gets misinterpreted in the printf since you're using "%s"; if you tried "%c" instead, you should see 'B' printed -- I have no idea what you're getting now.
This should work:
Note that I changed the return type to char* and I removed the * in the return statement that dereferenced msg. Also, I corrected the declaration of main and added the requisite return from main.
char *pass(char msg);
char *pass(char msg)
Look it over, think it over, and understand what's happening.
February 13th, 2013, 05:22 PM
That worked. Thank you.
But I'm confused why you put a pointer by the function.
Why did you put a pointer there? I didn't see you use an address of operator so does the function have its own address?
Can you just please explain why you put a pointer there? That'll be great! ;)
February 13th, 2013, 05:56 PM
is the same as
and even as
It's not saying anything about being a pointer to pass, but rather just says that this is a char pointer datatype. It's most common to write it as
even though some like to place it with the char to make that more clear.
I think the common way is due to multiple variable declarations on a single line; eg:
char ch, *cp;
That declares two variables: ch which is a char and cp which is a char pointer. As you can see, the * is associated with the variable name there, so that appears to be carried over to other declarations; eg, function parameter lists, function return types.
So to reiterate, there's no difference between char *pass and char* pass. They both say the exact same thing.
Last edited by dwise1_aol; February 13th, 2013 at 06:01 PM.
February 14th, 2013, 07:16 AM
char *password(char pass)
printf("Enter password you want");
printf("you entered %s",pass);
Comments on this post
February 14th, 2013, 11:00 AM
Blam! I just blew your program out of the water! Or as Da-Wei would put it, I sent it running off into the weeds to puke all over its own shoes.
I'm quite sure that we've already warned you about using gets. I also know that most documentation on gets warns you to not use it (I know that most man pages do) and that some compilers will issue a warning when you use it (or are you ignoring warnings?).
And I'm puzzled by your giving the array's dimension in the function header like that. Frankly, in more than 20 years experience with C/C++ I've not seen that done outside of a few people on this forum. I assume that it must work, but I don't see how it would make much difference.
Here is a much better version:
#define MAX_PASS_LEN 40
char *password(char *pass)
// fgets limits the input to MAX_PASS_LEN-1, thus
// automatically accounting for the null-terminator
fgets(pass, MAX_PASS_LEN, stdin);
// normally, we would need to make pass longer than 1 in order to
// accommodate the null-terminator, but fgets handles that for us
printf("Enter password you want: ");
// what's actually happening here is that we are throwing away
// the return value and receiving the change through the char
// pointer we passed to the function (array name acts as a pointer)
// However, if we had done this:
// printf("you entered %s\n",password(pass));
// then we would still have received the change the same way,
// but now the returned char pointer is used by printf.
// This is just about the only reason to return the pointer,
// but it is a good reason and hence good practice.
printf("you entered %s\n",pass);
// only needed if you aren't running this from the shell's command line.
My reply was primarily to address your implicit recommendation to use gets. Here is what the GNU C Library help file says:
When a buffer overflow occurs, the memory after the buffer get overwritten, AKA "clobbered". When this happens inside of a function (even main is a function), then that can destroy information that it vital to the execution of that function, including the address that it is to return to. This will most commonly lead to the program crashing.
But even worse, such careless programming leaves your program open to buffer overflow exploits, a common method for hackers to take over your system. Sure, the most malicious thing a hacker could do to your student program is to cause it to crash, but if you continue your careless practices in writing commercial software, then a hacker will find that buffer overflow and create an exploit tailored to it, one which will "return" to code embedded in the overflow input, the "payload", that will either take over or destroy that system. At best, it will allow the hacker to do a denial of service attack by causing the system to crash.
Learn as early as you can to program correctly and robustly.
Last edited by dwise1_aol; February 14th, 2013 at 12:05 PM.