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

    Join Date
    Nov 2012
    Posts
    23
    Rep Power
    0

    Converting arrays to pointers


    I have a homework problem where I need to convert a function written with arrays to one with character pointers. The code is below. Im confused on how to even begin. Any pointers? get it... pointers?? ...lol... :rolleyes:

    Code:
    // Function to compare two character strings
    int compareStrings (const char s1[], const char s2[])
    {
    int i = 0, answer;
    while ( s1[i] == s2[i] && s1[i] != '\0'&& s2[i] != '\0' )
    ++i;
    if ( s1[i] < s2[i] )
    answer = -1; /* s1 < s2 */
    else if ( s1[i] == s2[i] )
    answer = 0; /* s1 == s2 */
    else
    answer = 1; /* s1 > s2 */
    return answer;
    }
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,895
    Rep Power
    481
    I've got pointers.
    Read about pointers and arrays from a few different sources. Here's a source that I didn't read.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,174
    Rep Power
    2222
    With a few important exceptions, array names are equivalent to pointers. So you can refer to an array by name as a pointer, which means that the first step would be to replace the array notation in the parameter list with pointer notation.

    Now, when you refer to an array with a pointer, you can still use array subscripting with that pointer, so just changing the parameters from array notation to pointer notation would work. However, I assume that you're also required to replace the subscripting in the function with pointers. I customarily declare two local char pointers, initialize them to the two pointer parameters, and then dereference them for comparisons and increment them to work my way through the strings.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Posts
    33
    Rep Power
    3
    fundamentally arrays and pointers are very alike except arrays are like constant pointers i.e. non-dynamic. pointers are free to reference memory anywhere. consider this:

    Code:
    int main()
    {
    int a[5];
    int *p;
    
    
    //ways you can create values for this array
    
    p = a; // same thing as p = &a;
    
    *p = 10; //the value pointed to by p (anArray[0]) = 10
    p++; 
    *p = 20;  // a[1] = 20
    p = &a[2];
    *p = 30; // a[2] = 30
    p = number + 3;
    *p = 40; // a[3] = 40
    p = numbers;
    *(p+4) = 50; // a[4] = 50
    }
    If you were to loop through this array it would obvious give you

    10, 20, 30, 40, 50,

    hope this helps

    Comments on this post

    • C505 agrees : Thank you for your help
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    156
    Rep Power
    34
    object[42] is exactly the same things as *(object + 42) whether object is an array or a pointer. So where you have an array indexing, replace with a pointer dereference.

    Code:
    /* ********
    while ( s1[i] == s2[i] && s1[i] != '\0'&& s2[i] != '\0' )
    ++i;
    ******** */
    while ( *(s1 + i) == *(s2 + i) && *(s1 + i) != '\0'&& *(s2 + i) != '\0' )
    ++i;
    But don't ever think arrays and pointers are the same thing!
    I suggest you read section 6 of the comp.lang.c FAQ (in fact, all sections).

    Comments on this post

    • C505 agrees : Thank you for your help
    Last edited by bdb; November 10th, 2012 at 02:31 PM.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    23
    Rep Power
    0
    Thanks to all for the replies and the links. I should have RTFM a bit more before posting, I appreciate the help.
    Last edited by C505; November 11th, 2012 at 11:01 AM. Reason: typo
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    23
    Rep Power
    0
    I didn't write the comparestrings function, it is from a textbook. I haven't come across a function that wants a const char as an argument, I do not understand how to call the function. I have tried calling it with arrays, calling it with integers, and chars, but I always get a compile error. Before I attempt to convert it to a function of pointers I would like to have statements to print values and addresses to keep track of them, but I can not even get the output to print. I need a little nudge, any help would be most appreciated.


    Code:
    #include <stdio.h>
    
    int compareStrings (const char s1[], const char s2[])
    {
    int i = 0, answer;
    while ( s1[i] == s2[i] && s1[i] != '\0'&& s2[i] != '\0' )
    ++i;
    if ( s1[i] < s2[i] )
    answer = -1; /* s1 < s2 */
    else if ( s1[i] == s2[i] )
    answer = 0; /* s1 == s2 */
    else
    answer = 1; /* s1 > s2 */
    return answer;
    }
    
    
    int main()
    {
    int result;
    result = compareStrings(1,2);    // How to format argument? 
    
    //Compile error here
    //[Warning] passing arg 1 of `compareStrings' makes pointer
    // from integer without a cast  Whats a cast?
    
    printf("answer is %i\n", result);  
    }
  14. #8
  15. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,895
    Rep Power
    481
    You lack fundamental concepts, so many it's hard to choose one. I choose
    ASCIIZ
    as being immediate though far from most critical.

    compareStrings("this","that");
    [code]Code tags[/code] are essential for python code and Makefiles!
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    23
    Rep Power
    0
    Originally Posted by b49P23TIvg
    You lack fundamental concepts, so many it's hard to choose one.
    lol.... clearly. However you could have sent me a link for the history of the breakfast burrito and I would have gotten more out of it. I read the wiki article, I dont see how it applies... maybe choose another fundamental concept to verse me on?
  18. #10
  19. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,895
    Rep Power
    481
    You called the function intended for string arguments with integers.

    compareStrings("this","that");


    The argument "this" passes a pointer as a function argument.

    1 gets converted to a pointer and the pointer 1 is passed to the function.


    When compareStrings looks for the first character of s1 it dereferences either the pointer to the ASCIIz string "this", which makes sense, or, with your use it dereferences (looks in the memory location 1) which the function would treat as an ASCIIz string if it had the chance. I'm pretty sure my operating system (and yours as well) will raise a memory fault upon dereferencing memory location 1 without superman privilege.
    [code]Code tags[/code] are essential for python code and Makefiles!
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    23
    Rep Power
    0
    I understand what the function wants, I really do. I just dont know the syntax. I will look it up. Thanks for the help.

    As far as superman goes, if I was superman id de-reference the whole damm thing with some laser vision or something. I mean the guy got shot in the eye, and the bullet bounced off, and I doubt he knows the first thing about compilers.
  22. #12
  23. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,895
    Rep Power
    481
    Sorry, all my fault. I actually thought that by
    result = compareStrings(1,2);
    you were trying to use your function to compare integers 1 and 2. Ha hahah!

    These fancy new intel chips actually have some sort of machine instruction to enable "super duper privilege" allowing access any part of memory, as I dredge up a memory from the last millennium.
    [code]Code tags[/code] are essential for python code and Makefiles!
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    23
    Rep Power
    0
    Would you mind checking this code and telling me what you think? It will compile but crashes after the first scanf. (The first block is commented out, to refer to the original program)

    Code:
    #include <stdio.h>
    #include <stdbool.h>
    #include <conio.h>
    
    // convert program to pointers
    
    /* int compareStrings (const char s1[], const char s2[])
    {
       int i = 0, answer;
       while ( s1[i] == s2[i] && s1[i] != '\0'&& s2[i] != '\0' )
           ++i;
       if ( s1[i] < s2[i] )
           answer = -1; 
       else if ( s1[i] == s2[i] )
           answer = 0; 
       else
           answer = 1; 
       return answer;
    }
    */
    
    int compareStrings (char *s1, char *s2)
    {
       int i = 0;
       int answer;
       
       while ( *(s1+i) == *(s2+i) && *(s1+i) != '\0'&& *(s2+i) != '\0' )
           ++i;
       if ( *(s1+i) < *(s2+i) )
           answer = -1; 
       else if ( *(s1+i) == *(s2+i) )
           answer = 0; 
       else
           answer = 1; 
       return answer;
    }
    
    
    int main()
    {
    int result;
    char s1[5];
    char s2[5];
    printf("\n compare strings");
    printf("\n enter string 1: ");
    scanf("%s,*s1");
    printf("\n enter string 2: ");
    scanf("%s,*s2");
    result = compareStrings(s1,s2);   
    printf(" answer is %i\n", result);  
    getch();
    }
  26. #14
  27. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,895
    Rep Power
    481
    scanf("%4s",s1);

    The 4 prevents buffer overrun.
    The " moved to end the format string.
    s1 is an address.
    *s1 would be an unassigned character, and I can't think of a sufficiently strong slang nasty to write about that. Reread the dwise, bdb, and puritylake posts you praised.
    [code]Code tags[/code] are essential for python code and Makefiles!
  28. #15
  29. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    23
    Rep Power
    0
    Originally Posted by b49P23TIvg
    scanf("%4s",s1);

    The 4 prevents buffer overrun.
    The " moved to end the format string.
    s1 is an address.
    *s1 would be an unassigned character, and I can't think of a sufficiently strong slang nasty to write about that. Reread the dwise, bdb, and puritylake posts you praised.
    b49P23TIvg,

    You fricken crack me up buddy. Not sure if you are a clever sarcastic, or just an a ss. But either way this has been a lot of fun, thanks so much for helping me. I can turn in my working program now. Id buy you a beer if you were around but since you only exist on the internet here is a drunk smiley. :cadrunk:

IMN logo majestic logo threadwatch logo seochat tools logo