Thread: Third newbie question

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    111

    Third newbie question

    This method takes two strings, then first compares the length and then uses memcmp to compare them if the length is identical.

    Code:
    int strcomp(char, char);
    
    int strcomp(char s1, char s2) {
            int l1 = strlen(s1);
            int l2 = strlen(s2);
            if (l1 == l2) {
                    return memcmp(s1, s2, l1);
            } else {
                    return 0;
            }
    }
    When compiling, it gives the following warning:

    test.c:25: warning: passing argument 1 of ‘strlen’ makes pointer from integer without a cast
    test.c:26: warning: passing argument 1 of ‘strlen’ makes pointer from integer without a cast
    test.c:28: warning: passing argument 1 of ‘memcmp’ makes pointer from integer without a cast
    test.c:28: warning: passing argument 2 of ‘memcmp’ makes pointer from integer without a cas
    t

    I've barely started looking at what pointers are yet, but I didn't think I "made" any pointers in this code.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should #include <string.h>.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    60
    You didn't 'make' any pointer but you're using a function that takes a pointer as an argument:
    Code:
    size_t strlen ( const char * str );
    str is a pointer to char.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    char is one character only - not a string
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    111
    Laserlight: Thanks - I'm including that, though.

    Hm. I guess I need to understand pointers before I use these methods, then. I haven't understood where and WHY to use pointers yet. Isn't a pointer a reference to the ADDRESS of a variable? So that if you f.ex. print out a pointer that points to a string ( char[5] = "Name"; *pchar = &char; ) it will print the address of the "char" var and not the string it contains?

    vart: Yah, I forgot to make arrays, it seems. But I get an error if I do method(char[10] s1, char[10] s2)...
    Last edited by cnewbie1; 12-01-2009 at 09:46 AM.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Pointer are variables that hold addresses, instead of int or char values.

    We don't print the actual addresses the pointers hold, too often. (You can do it, but it's rare that you need to). You can also use the pointer to print out what the pointer has the address of. THAT gets used all the time.

  7. #7
    Registered User
    Join Date
    Nov 2009
    Posts
    111
    Could you give me a hint on how to write the obove method correctly, then? I kick-in-the-butt to get to understand pointers? :-)

  8. #8
    Registered User
    Join Date
    Nov 2009
    Posts
    60
    A hint, but you should try to implement it yourself:

    Code:
    int strcomp(char *, char *);
    or even better:
    Code:
    int strcomp(const char *, const char * );
    because the function is not supposed to change the strings.

    Try to write the implementation yourself and post it here.

  9. #9
    Registered User
    Join Date
    Nov 2009
    Posts
    60
    Another important fact to know is that if

    Code:
    char a[6] = "hello";
    int i = 0;
    then these two statements are the same: a[i] and *(a + i)

    This means that an array name actually is a pointer! This is very important to know. The following explains this:

    If you declare :

    Code:
    char a[6] = "hello";
    char * p;
    then p = a is the same as p =&a[0]

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by hilarius
    This means that an array name actually is a pointer!
    No, an array is converted to a pointer to its first element in that context.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Nov 2009
    Posts
    60
    Quote Originally Posted by laserlight View Post
    No, an array is converted to a pointer to its first element in that context.
    Should I have said: "An array name is an address?" Is this correct? In any case you can do pointer arithmetics with an array name (like *(a+1) in my example). This let me make the conclusion that an array name is equivalent with a pointer.

    (but you look much more experienced then me. I'm a beginner myself)

  12. #12
    Registered User
    Join Date
    Nov 2009
    Posts
    111
    Ok, I will try. But first - why does this compile without warning?
    Code:
            char str[10];
            printf("%s is %d characters long\n", str, strlen(str));
    There's no pointer passed to strlen() here...?

  13. #13
    Registered User
    Join Date
    Nov 2009
    Posts
    60
    Quote Originally Posted by cnewbie1 View Post
    Ok, I will try. But first - why does this compile without warning?
    Code:
            char str[10];
            printf("%s is %d characters long\n", str, strlen(str));
    There's no pointer passed to strlen() here...?
    There is a pointer passed: in my post above I said that array names are equivalent with pointers. If you take str, it is seen as the address of the first element of the array.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by hilarius
    Should I have said: "An array name is an address?" Is this correct?
    No, that would also be inaccurate, for the same reason. For example, take the sizeof an array and take the sizeof a pointer to an element of that array. Except by coincidence, the results will be different.

    Quote Originally Posted by cnewbie1
    There's no pointer passed to strlen() here...?
    Since the array named str is converted to a pointer to its first element, there is indeed a pointer passed to strlen().
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    Nov 2009
    Posts
    60
    Laserlight, you are right:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
            char a[6] = "hello";
    	char * p = a;
    	printf("%d\n", sizeof(a));
    	printf("%d\n", sizeof(p));
    	return 0;
    }
    gives 6 and 4. I'm a bit confused now. I'll look it up in the documentation. Thanks for the hint!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie question, C #
    By mate222 in forum C# Programming
    Replies: 4
    Last Post: 12-01-2009, 06:24 AM
  2. Stupid Newbie question
    By TimL in forum C++ Programming
    Replies: 4
    Last Post: 07-22-2008, 04:43 AM
  3. C prog newbie question
    By Draginzuzu in forum C Programming
    Replies: 1
    Last Post: 02-03-2003, 06:45 PM
  4. a stupid question from a newbie
    By newcomer in forum C++ Programming
    Replies: 4
    Last Post: 01-11-2003, 04:38 PM
  5. newbie class templates question
    By daysleeper in forum C++ Programming
    Replies: 2
    Last Post: 09-18-2001, 09:50 AM