Thread: Simple pointer problem...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    58

    Simple pointer problem...

    Hi everyone

    I an trying to get the size of a string that is being held inside a variable defined as a pointer, I keep getting either one or two from the following code, where as I want to get 11 (number of letters in “hello world”)

    Can someone help me out please,

    many thanks

    David

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int size = 0;
    char *message;
    int main()
    {
        message = "hello world";
        size = sizeof(message);
        printf("size: %i", size);
        return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    message is a pointer, so sizeof(message) returns the size of a pointer. #include <string.h> and use strlen(message) instead.

    By the way, unless there is some special need to make size and message global variables, make them local variables.
    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
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    you want strlen() not sizeof().

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    58
    Ah! fantastic - thanks

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I keep getting either one or two from the following code
    Are you by any chance using Turbo C++? If so, I would suggest that you get a compiler produced in this decade. 10+ years old may be fine for Whiskey, Cognac, etc, but it's not really a quality mark on a compiler.

    The Microsoft Visual Studio C++ Express Edition is available for free, and does everything that Turbo C++ does, and lots more.

    Or if, you don't like Microsoft, Code::Blocks or Dev-C++ are two packages that use gcc as a compiler. gcc is the compiler used to build Linux, and it's a really great open-source compiler [meaning that you can get the source code for that compiler - but it's NOT even close to beginner code, so I would suggest that you don't try that unless you have plenty of spare time and already know LOTS of C (and a bit about compiler design and such)].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Also, if you plan on storing string literals in pointer, I suggest you make them const char. This explain it somewhat more clearly:
    http://cpwiki.sourceforge.net/Common...kes_and_errors
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Aug 2008
    Posts
    2
    be careful!!
    you are not allocating the memory

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by ari View Post
    be careful!!
    you are not allocating the memory
    And neither is there any need for that in the code posted.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A problem with pointer initialization
    By zyklon in forum C Programming
    Replies: 5
    Last Post: 01-17-2009, 12:42 PM
  2. Problem with function's pointer!
    By Tirania in forum C Programming
    Replies: 5
    Last Post: 11-28-2008, 04:50 AM
  3. printf output makes no sense. Is it a Pointer problem?
    By officedog in forum C Programming
    Replies: 3
    Last Post: 10-03-2008, 09:01 AM
  4. Simple Pointer Problem
    By Atlas24 in forum C++ Programming
    Replies: 7
    Last Post: 11-29-2005, 01:34 AM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM