#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Questioner's home
    Posts
    89
    Rep Power
    11

    C pointer & ascii pob, need help


    I'm trying to write a tiny c procedure that accepts a string from user and then add up all the ascii value of the string and display it out, I tried and tried and cant seem to get it right, now I'm really tired, and I still cant get it to work. I think my prob is I dunno how to use pointers to correctly point the values. I tried my best to not asking for help in here, but I really cant do it myself. My procedure is like this:

    char * str;

    void AS()
    {
    int asc, asci = 0;
    int *ptr;
    ptr = &asc;

    system("cls");

    printf("Enter a string : ");
    scanf("%s", &str);

    for(ptr = 0; ptr != '\0'; ptr++)
    {
    asci = asci + ptr;
    }

    printf("The total number is %d\n", asci);
    system("pause");
    }

    Thnx for your help.
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Your assessment is correct: the problems are pointer-related.

    (corrections in bold)
    Code:
    char str[81]; 
    
    void AS() 
    { 
        int asc, asci = 0; 
        char *ptr; 
        ptr = &asc; 
    
        system("cls"); 
    
        printf("Enter a string : "); 
        scanf("%s", str); 
    
        for(ptr = str; *ptr != '\0'; ptr++) 
        { 
            asci = asci + *ptr; 
        } 
    
        printf("The total number is %d\n", asci); 
        system("pause"); 
    }
    NOTES:
    char str[81];
    str needs to have memory allocated to it. Either change it to an array as I have here, or malloc some dynamic memory to it before you try to use it. Otherwise, you would get a segmentation fault.

    char *ptr;
    Since ptr will point to characters in a char array, it seemed more fitting to change the data type of ptr. In arithmetic operations, a char is treated as a numeric value anyway, ie as its ASCII value.

    int asc , asci = 0;
    ptr = &asc;
    The variable, asc, is not used.

    scanf("%s", str);
    scanf requires a pointer to the variable that will receive the scanned value. But str is already a pointer (array names are equivalent to pointers.

    for(ptr = str; *ptr != '\0'; ptr++)
    For ptr to scan through the string in str, it needs to be initialized to pointing to the beginning of str (or wherever you want to start within str).
    ptr contains the address of a value, not the value itself, so to get to that value you need to dereference the pointer. Besides, if ptr had the value of '\0', then it would be NULL and attempts to use it would result in a segmentation fault.

    asci = asci + *ptr;
    Again, you want to use the value that ptr points to, not the value of ptr itself, so you need to dereference it.
    An alternative and very C-ish way to write that would be:
    asci += *ptr;
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Questioner's home
    Posts
    89
    Rep Power
    11
    Thanks a lot, I really appreciate it, I never thought the reply i get will be this detailed, really helpful! Thanks again.
  6. #4
  7. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    yea dwise rocks i wish my teachers were as clear and thorough as he is :)

IMN logo majestic logo threadwatch logo seochat tools logo