Thread: beginner help with C (pointers)

  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
    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’

  7. #7
    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.

  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
    I would love to understand what yall are talking about, can you post with a more kindergarten style so the beginner can learn?

  10. #10
    Registered User
    Join Date
    Feb 2009
    Location
    USA, south
    Posts
    10
    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 = strdup(s);
        int i;
        for(i = 0; p[i]; i++)
            p[i] = toupper(p[i]);
        return p;
    }
    after trying to complie:
    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’

  11. #11
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    your code is already kindergarten style. you asked for something fancier, and that's what malloc does for you. i also fixed the code in my first post, so you can try it again.

  12. #12
    Registered User
    Join Date
    Feb 2009
    Location
    USA, south
    Posts
    10
    ok noted the error in yalls replys. fixed and it compiles.. but i still don't unerstand why. can you add comments describing what each line preforms?

  13. #13
    Registered User
    Join Date
    Feb 2009
    Location
    USA, south
    Posts
    10
    im not asking for kindergarten code, im asking for you to explain what the code does to me as a beginner.

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by likewise View Post
    ok noted the error in yalls replys. fixed and it compiles.. but i still don't unerstand why. can you add comments describing what each line preforms?
    It would be better if you asked for comments on lines that are particularly difficult for you to understand - commenting thirty or so lines of code, when there are perhaps 5 that you don't understand is a waste of EVERYONE's time.

    --
    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
    Feb 2009
    Posts
    138
    Quote Originally Posted by likewise
    can you add comments describing what each line preforms?
    yes i can.
    Code:
    #include <stdio.h>  /* pastes contents of stdio.h to here */
    #include <stdlib.h> /* pastes contents of stdlib.h to here */
    #include <string.h> /* pastes contents of string.h to here */
    #include <ctype.h>  /* pastes contents of ctype.h to here */
    /* blank line */
    char *sdup(char* s); /* declares a function called sdup */
    char *supper(char *s); /* declares a function called supper */
    /* blank line */
    int main() /* definition of main */
    { /* opening curly brace */
        char s[80], *p; /* declare an array of 80 char and a pointer to char */
        printf("enter a string> "); /* print something cool */
        fgets(s, 80, stdin); /* make the user type a string, up to 80 characters */
        fputs(s, stdout); /* print the string the user typed */
        p = sdup(s); /* make a copy of the string the user typed */
        fputs(supper(p), stdout); /* convert the copy to upper case and print it */
        free(p); /* let the dynamic memory go back to the process */
        return EXIT_SUCCESS; /* return succes */
    } /* closing curly brace */
    /* blank line */
    char *sdup(char* s) /* definition of sdup, copies strings */
    { /* opening curly brace */
        char *p = malloc(strlen(s)+1); /* dynamically allocate strlen(s)+1 bytes */
        return strcpy(p, s); /* copy the string contained by s into the memory at p and return it */
    } /* closing curly brace */
    /* blank line */
    char *supper(char *s) /* definition of supper, copies a string to upper case */
    { /* opening curly brace */
        char *p = sdup(s); /* copy the string s */
        int i; /* int i */
        for (i = 0; p[i]; i++) p[i] = toupper(p[i]); /* convert the string to upper case */
        return p; /* return the copy */
    } /* closing curly brace */

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