Thread: beginner help with C (pointers)

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Feb 2009
    Location
    USA, south
    Posts
    10

    beginner help with C (pointers)

    I am a C beginner. I just created a program that iterates over the length of a string that calls toupper() on each character.

    My question: I want to create a copy of the original, translate that and then return a pointer to the translated copy... and I am having troubles.

    Anyone up for helping?

    Thanx.

  2. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    i can't help if you don't show your code and explain what the problem is.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Location
    USA, south
    Posts
    10
    Quote Originally Posted by Meldreth View Post
    i can't help if you don't show your code and explain what the problem is.
    ok sry, I am also on a mac. This is the code:
    Code:
    #include <stdio.h>
    
    main()
    {
            char firstname[80];
            int loop =0;
    
            printf(" Enter your name in lowercase letters: \n");
            scanf(" %s", firstname);
            for(; firstname[loop] != 0; loop ++ )
                    firstname[loop] = toupper(firstname[loop]);
    
            printf("Your name in uppercase letters is: %s\n", firstname);
    }
    There aren't any problems, i just want to learn how to use the function of toupper() in a fancier way. A friend said i could have it create a copy of the original, translate that and then return a pointer to the translated copy... but as a beginner I am confused by this. Friend is currently out of pocket.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    there's nothing fancy about c. your friend just means to copy the string before converting the case. it doesn't do anything but add an extra step that you probably don't need.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    char *sdup(char* s);
    char *supper(char *s);
    
    int main()
    {
        char s[80], *p;
        printf("enter a string> ");
        fgets(s, 80, stdin);
        fputs(s, stdout);
        p = supper(s);
        fputs(p, stdout);
        free(p);
        return EXIT_SUCCESS;
    }
    
    char *sdup(char* s)
    {
        char *p = malloc(strlen(s)+1);
        return strcpy(p, s);
    }
    
    char *supper(char *s)
    {
        char *p = sdup(s);
        int i;
        for (i = 0; p[i]; i++) p[i] = toupper(p[i]);
        return p;
    }
    Last edited by Meldreth; 02-09-2009 at 02:34 PM.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Meldreth: Being pedantic, but:
    You declare and define a funciton called "sdup", then call strdup() which is a non-standard but common library function.

    Also, I very much prefer a variant where you pass in the receiving string, e.g.
    Code:
    char *supper(const char *in, char *out, int maxlen)
    {
         char *s = out;
         while(*in && s - out < maxlen-1)
         {
             *s++ = toupper(*in);
             in++;
         }
         *s = 0;
         return out;
    }
    The maxlen parameter is there to prevent buffer overflows.

    The advantage here is that the user has the choice of using a dynamically, stack or static allocation for the receiving variable. Returning the original is useful if you want to do stuff like:
    Code:
       char something[100];
       char temp[4];
       const char *abc = "abc";
       strcpy(something, abc);
       strcat(something, supper(abc, temp, sizeof(temp)));
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    Quote Originally Posted by matsp
    You declare and define a funciton called "sdup", then call strdup()
    my bad. i got tongue lashed by laserlight for using strdup before, so i changed my mind half way and didn't follow through with it. i fixed it.
    Last edited by Meldreth; 02-09-2009 at 02:36 PM.

  7. #7
    Registered User
    Join Date
    Feb 2009
    Location
    USA, south
    Posts
    10
    as a beginner, that coding does not flow easily. Will you repost with comments about what each line does? I want to avoid anything needing ctype.h because with my mac working in terminal, many functions are not part of the standard.

    I cannot get code to compile:

    Code:
    $ gcc -o caps caps.c
    caps.c: In function ‘supper’:
    caps.c:10: error: syntax error before ‘{’ token
    caps.c:31: error: syntax error before ‘for’

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by likewise View Post
    as a beginner, that coding does not flow easily. Will you repost with comments about what each line does? I want to avoid anything needing ctype.h because with my mac working in terminal, many functions are not part of the standard.

    I cannot get code to compile:

    Code:
    $ gcc -o caps caps.c
    caps.c: In function ‘supper’:
    caps.c:10: error: syntax error before ‘{’ token
    caps.c:31: error: syntax error before ‘for’
    ctype.h is part of the standard library, and as such, should work on Mac, Linux, Windows, OS/2, Solaris and just about any other mainstream (and many not-so-mainstream) OS you can think of.

    Since I can't see anything directly wrong in the code posted, it's probably best if you post what you are trying to compile, and we can probably help you out.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Feb 2009
    Location
    USA, south
    Posts
    10
    Then you didn't understand how much of a beginner I was.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by likewise View Post
    Then you didn't understand how much of a beginner I was.
    So, can you explain HOW supper() works now?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Feb 2009
    Location
    USA, south
    Posts
    10
    No I cannot, but I have a start. I was looking for help, not an aggressive attack of sarcasm to put me down. Which makes me think the both of you have insecurities of some measure where you feel the need to protect yourself with sarcastic remarks that only let the world see a superficial part of who you are.

    I am a beginner, 100% and if you do not want to waste your time with me, then please don't.

  12. #12
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    oh, i'm sorry i tried to help you then. i won't make that mistake again.

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by likewise View Post
    No I cannot, but I have a start. I was looking for help, not an aggressive attack of sarcasm to put me down. Which makes me think the both of you have insecurities of some measure where you feel the need to protect yourself with sarcastic remarks that only let the world see a superficial part of who you are.

    I am a beginner, 100% and if you do not want to waste your time with me, then please don't.
    If you want careful, detailed explanations of things, why are you on a forum (where you can get anything you want except careful, detailed explanations -- no one wants to type that much and end up just typing up the first 15 pages of a C textbook)? We can help with short, specific questions easily, because they have nice short answers; we can point you to things to read if you want things to read (see e.g. the book sticky on the top of the forum). We're not going to type up a chapter of a book into this little box just for you, though.

    And don't worry -- the sarcasm is not to put you down, but point up the futility of the request (it's just as impossible to read way-overly-commented code as uncommented code). No one here is taking this personally, even me.

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by likewise View Post
    No I cannot, but I have a start. I was looking for help, not an aggressive attack of sarcasm to put me down. Which makes me think the both of you have insecurities of some measure where you feel the need to protect yourself with sarcastic remarks that only let the world see a superficial part of who you are.

    I am a beginner, 100% and if you do not want to waste your time with me, then please don't.
    I don't think I have made sarcastic comments - I honestly wanted to know if you had actually gained understanding from the comments posted. However, I expect this comment is in vain, as you appear to have found some other people to try to get meaningful comments out of.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    This is a cross post

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Replies: 17
    Last Post: 03-03-2005, 06:47 PM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. Pointers to function(function pointers)
    By abhishek_charli in forum C Programming
    Replies: 4
    Last Post: 06-23-2002, 01:24 AM
  5. Major Beginner looking for simple pointers
    By dlkchic in forum C++ Programming
    Replies: 4
    Last Post: 09-05-2001, 12:27 PM