[SOLVED] Pointer and Data Type Trouble [User input] {C Code}
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Pointer and Data Type Trouble [User input] {C Code}
I am writing a simple program as part of my course work and I am having a very annoying time right now. It is a pig latin translator and at the moment I am trying to get it to do very basic things. Take in a word from stdin and then output that word. I am having a HUGE headache with this. I am wanting to get in the habit or writing minimal and correct code, thus I am using pointers. Believe it or not the pointers aren't being the biggest annoyance at the moment, it's data types. I have this code:
pig.c: In function 'pig_translate':
pig.c:11: warning: function returns address of local variable
pig.c: In function 'main':
pig.c:20: error: lvalue required as left operand of assignment
pig.c:22: warning: passing argument 1 of 'pig_translate' from incompatible pointer type
I know I am likely missing something extremely basic, but this is confusing me beyond no belief. I can not for the life of me figure out why this isn't working. From what I can tell error on line 22 is due to the fact that c is an int, however if it is a char then I get an error about it being assigned to getchar() which is stored as an int. Is there another method of gathering user input for chars that I am missing? I have no ideas about line 20, I tried giving c 1024 of memory to see if that would satisfy this error but no luck. Please help, I am extremely eager to understand why this is wrong so I can further trouble shoot these errors in the feature.
Last edited by anon033; 04-05-2019 at 12:27 PM.
Reason: Specifying Language
It's warning about returning output[]. You could define a pointer to output, a char * and have that point to output:
Code:
char *pOut = output;
And then return pOut. That should fix all of the function.
It's complaining about the use of getchar(). If you check the manual page for getchar() you'll see that it returns an int, not a pointer to an int. Further, you've declared c to be a pointer to an int and then you use the address of that. Neither are good. Make variable 'c' not a pointer, just an int, and then do not specify the address of it for the return from getchar(), instead it should be:
Code:
int c;
c = getchar();
Since pig_translate is returning a pointer to character, then this can be considered to be a string, even if you are only passing back the pointer to one character. The compiler cannot tell here, so instead of using %c, you would use %s to designate a string modifier for the printf() format string.
Finally, the complaint that the argument is from an incompatible variable type for the call to pig_translate. It is because you've defined the prototype for pig_translate to be char *pig_translate(char *input). Thus when you call it as you've coded, you have it passing the address of an integer. You can cast that variable to be a char pointer:
Code:
(char *)&c
See if some of these suggestions work and also please do take the time to review things like what the code is actually saying, where you need to match variable types correctly. Also please be sure to check things like the manual page for C library functions using the man pages.
@OP: Maybe we could suggest something if we knew what you wish to do. (The term 'pig translation' might be meaningful for yourself, but I have no idea what it is.)
@OP: Maybe we could suggest something if we knew what you wish to do. (The term 'pig translation' might be meaningful for yourself, but I have no idea what it is.)
The OP wishes to translate English to Pig Latin. It's where you take the leading consonants of a word up to the first vowel I believe and attach it to the end of the word and then add 'ay', so Pig Latin would translate to:
you see, when you call 'pig_translate()' the 'output[]' array is created on the +stack+
so you mess about, and when you return the stack is discarded, so output[] is now what is known as "undefined".
and the calling function is looking on the stack of a function that has 'gone away', no longer exists.
depending on your luck it may work on certain machines on certain occasions.
The usual paradigm is to pass a pointer and size to fill up
It's warning about returning output[]. You could define a pointer to output, a char * and have that point to output:
Code:
char *pOut = output;
And then return pOut. That should fix all of the function.
It's complaining about the use of getchar(). If you check the manual page for getchar() you'll see that it returns an int, not a pointer to an int. Further, you've declared c to be a pointer to an int and then you use the address of that. Neither are good. Make variable 'c' not a pointer, just an int, and then do not specify the address of it for the return from getchar(), instead it should be:
Code:
int c;
c = getchar();
Since pig_translate is returning a pointer to character, then this can be considered to be a string, even if you are only passing back the pointer to one character. The compiler cannot tell here, so instead of using %c, you would use %s to designate a string modifier for the printf() format string.
Finally, the complaint that the argument is from an incompatible variable type for the call to pig_translate. It is because you've defined the prototype for pig_translate to be char *pig_translate(char *input). Thus when you call it as you've coded, you have it passing the address of an integer. You can cast that variable to be a char pointer:
Code:
(char *)&c
See if some of these suggestions work and also please do take the time to review things like what the code is actually saying, where you need to match variable types correctly. Also please be sure to check things like the manual page for C library functions using the man pages.
The OP wishes to translate English to Pig Latin. It's where you take the leading consonants of a word up to the first vowel I believe and attach it to the end of the word and then add 'ay', so Pig Latin would translate to:
Probably the cleanest thing to do would be to still use the integer, add a declaration for a character, cast the result of the getchar() to that, and then send the address of that char to the function.
Code:
int c;
char in_char;
c = getchar();
if( c != EOF ) {
in_char = (char)c;
printf("%s\n", pig_translate(&c));
}
Probably the cleanest thing to do would be to still use the integer, add a declaration for a character, cast the result of the getchar() to that, and then send the address of that char to the function.
Code:
int c;
char in_char;
c = getchar();
if( c != EOF ) {
in_char = (char)c;
printf("%s\n", pig_translate(&c));
}
Even using this I can't get that final issue resolved. I'll have to ask my teacher so he can explain it to me in detail. Thank you so much for your time! I am really wanting to learn C as best I can and fully understand the language. Sometimes I feel like I know nothing though
Write code and compile it with the most stringent warning and error checking flags enabled. Make sure you generate warning free code. Also, use the C99 standard, either use the std= flags for gcc or use c99 the compile it.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.