LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-04-2019, 04:30 PM   #1
anon033
Member
 
Registered: Mar 2019
Posts: 188

Rep: Reputation: 13
Unhappy 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:

Code:
#include <stdio.h>
#include <string.h>

char * pig_translate(char * input) {

        char output[64] = {0};
        char first_ch = input[0];

        strcpy(output, &input[1]);

        return output;

        char vowels[5] = {'a','e','i','o','u'};

}

int main() {

        int * c;
        &c = getchar();

        printf("%c\n", pig_translate(&c));
        return 0;
}
and this returns the following errors:

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
 
Old 04-04-2019, 05:44 PM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Well a couple of things:
  • 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.
 
1 members found this post helpful.
Old 04-04-2019, 08:06 PM   #3
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,783

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Quote:
Originally Posted by rtmistler View Post
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
This is not portable; while it will probably do what you expect on a little-endian machine like x86, I wouldn't recommend it.
 
1 members found this post helpful.
Old 04-04-2019, 11:23 PM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,868
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
@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.)
 
Old 04-05-2019, 12:16 AM   #5
Mechanikx
Member
 
Registered: Jul 2018
Distribution: Slackware
Posts: 351

Rep: Reputation: 258Reputation: 258Reputation: 258
Quote:
Originally Posted by NevemTeve View Post
@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:

Quote:
igPay atinLay

(but without capitalizing the 'P' and the 'L')
@FOSSilized_Daemon

Cool username

Last edited by Mechanikx; 04-05-2019 at 12:19 AM.
 
2 members found this post helpful.
Old 04-05-2019, 03:11 AM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,868
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Thank you. In this case OP should read the input word-wise (hint: isalpha), and create a function like this:
Code:
static void DoWord (const char from[], char into[]) /* 'into' has to hold strlen(from)+3 bytes */
{
    static const char vowels[] = "aeiou";
#define isvowel(c) (strchr(vowels,(c))!=NULL)
...
}

Last edited by NevemTeve; 04-05-2019 at 03:36 AM.
 
Old 04-05-2019, 06:22 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
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

Code:
char duh[1024];
pig_translate( duh, sizeof duh);
 
Old 04-05-2019, 10:48 AM   #8
anon033
Member
 
Registered: Mar 2019
Posts: 188

Original Poster
Rep: Reputation: 13
Quote:
Originally Posted by rtmistler View Post
Well a couple of things:
  • 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.
-------------------------------------------------------------------------------------------------------------------------------------

That'd do it! However, I am gonna try to see if there is a better way to do (char *)&c.
 
Old 04-05-2019, 10:49 AM   #9
anon033
Member
 
Registered: Mar 2019
Posts: 188

Original Poster
Rep: Reputation: 13
Quote:
Originally Posted by Mechanikx View Post
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:



@FOSSilized_Daemon

Cool username
Thank you
 
Old 04-05-2019, 01:13 PM   #10
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
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));
}
 
Old 04-05-2019, 05:36 PM   #11
anon033
Member
 
Registered: Mar 2019
Posts: 188

Original Poster
Rep: Reputation: 13
Quote:
Originally Posted by rtmistler View Post
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
 
Old 04-05-2019, 06:15 PM   #12
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Are you using a Linux system for programming?

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.

Nothing beats continued everyday work on things.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Is pointer a data-type or what aditya Programming 69 09-08-2017 02:09 PM
User input into Bash scripts and checking validity of user input?? helptonewbie Programming 8 07-07-2008 06:40 PM
Repeated "input: AT Translated Set 2 keyboard as /class/input/input" messages AcerKev Mandriva 2 09-16-2007 08:35 AM
list<type> how can I make type be a pointer? exodist Programming 2 06-06-2005 08:40 AM
returning data to main() via a pointer to a pointer. slzckboy Programming 3 05-30-2005 01:20 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:13 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration