Thread: Another newbie-question

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

    Another newbie-question

    Anothe newbie question, I'm afraid. (Shold be a forum called Total Newbies o something.)

    I might be a bit tired at the present time, but this I don't get:

    The following code:
    Code:
    char correctname[7] = "Myname";
    int correctlength = strnlen(correctname);
    printf("Name: %s\n", correctname);
    printf("Length: %d\n", correctlength);
    works like I expect it too. It creates a string, then creates an int variable with the string's length as value. Then it prints the string and the value. 100% obvious an basic.

    Output:
    Name: Myname
    Length: 6


    However, when I want to add another string:
    Code:
    char correctname[6] = "Frank";
    int correctlength = strnlen(correctname);
    char newname[10];       <--------------------------------- another string variable
    printf("Name: %s\n", correctname);
    printf("Length: %d\n", correctlength);
    suddenly, the int variable correctlength becoes zero.

    Output:
    Name: MyName
    Length: 0


    Wha-what-what?! By declaring another char variable, the unrelated int variable counting the length of the previous cha variable, is changed?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    #include<stdio.h>
    #include<string.h>
    int main( void )
    {
        char correctname[6] = "Frank";
        int correctlength = strlen(correctname);
        char newname[10];
        printf("Name: %s\n", correctname);
        printf("Length: %d\n", correctlength);
        
        return 0;
    }
    It works fine, assuming you actually use the right function strlen not strnlen.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Problem may stem from the fact that strnlen() is being invoked incorrectly ie it takes two arguments not one.

  4. #4
    Registered User
    Join Date
    Nov 2009
    Posts
    111
    Oh my. I WAS wondering a bit about that, but obviously I didn't wonder enough. Somehow I started using strnlen instead of strlen - not sure why. I guess i AM too tired :-)

    However - one last question: This line:

    Code:
    int correctlength = strlen(correctname);
    gives this warning:

    warning: incompatible implicit declaration of built-in function ‘strlen’;

    And I don't like warnings. How obvious is the answer to that?

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Did you include string.h as shown in my working example?


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Nov 2009
    Posts
    111
    Of course I didn't - when I was testing this exclusively. Thanks :-)

Popular pages Recent additions subscribe to a feed

Similar Threads

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