strlen() and passing by address

This is a discussion on strlen() and passing by address within the C++ Programming forums, part of the General Programming Boards category; Is the strlen function used this way? Code: Insert(char *s, int index) { int size_of_string = Size(); int length = ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    92

    strlen() and passing by address

    Is the strlen function used this way?
    Code:
    Insert(char *s, int index)
    {
       int size_of_string = Size();
       int length = strlen(s);
       int sum = size_of_string + sum;
    }
    If not what would be the alternative?

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    strlen takes the length of a char* string. I don't know what you're trying to do with your code. char* strings are passed by pointer when they're given as arguments. (No extra copy is made, so any modifications made to that string are still there when that function exits.)

  3. #3
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Info about strlen e.i. arguments and return values.

    Code:
    Insert(char *s, int index)
    {
       int size_of_string = Size();
       int length = strlen(s);
       int sum = size_of_string + sum;//This line is dangerous because sum is uninitialized
    }
    Try and print the variable sum. Not as expected? Why? Variable sum contains a garbage value. If this was a pointer variable you would have a serious BUG.
    Solution: Always initialize a variable when it is possible!
    01000111011011110110111101100100 011101000110100001101001011011100110011101110011 01100100011011110110111001110100 01100011011011110110110101100101 01100101011000010111100101110011 0110100101101110 01101100011010010110011001100101
    Good things donīt come easy in life!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Playing around strlen :)
    By audinue in forum C Programming
    Replies: 6
    Last Post: 06-13-2008, 03:22 PM
  2. Passing string length to another file
    By JFonseka in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 05:27 AM
  3. Trouble passing a string into strlen()...
    By yaya in forum C++ Programming
    Replies: 4
    Last Post: 01-08-2008, 03:46 AM
  4. Replies: 4
    Last Post: 03-31-2003, 11:49 PM
  5. passing strings and using strlen
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 09-15-2001, 10:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21