Thread: Beginner pointers/arrays question

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    2

    Beginner pointers/arrays question

    Hi all - apologies if this is a tired old question. I couldn't find anything on first glance in the forums or tutorials.
    I'm new to C, and I'm trying to find out from inside a function how many characters have been allocated in memory to a string array. The catch is that the function only has a pointer to the array that it takes as an argument. I've tried something like this:

    Code:
    #include <stdlib.h>
    
    int get_free_chars(char *ptr)
    {
        return sizeof(ptr)/sizeof(char);
    }
    
    main()
    {
        char foo[50] = "";
        int a = sizeof(foo) / sizeof(char);
        int b = get_free_chars(foo);
    }
    (Obviously there would be no point in the get_free_chars function alone - I'm trying to do something similar within a string manipulation function I'm coding)

    'a' takes the value I'd expect - 50 - but 'b' comes out to be 4 (obviously because the program is measuring the size of the pointer (4 bytes = 32 bits, which is the size for my system) rather than the array itself). Doing sizeof(*ptr) obviously wouldn't work as that would point directly to the first character only.

    Is there a nice way to get this information from within the function?

    Thanks in advance!
    Last edited by whiteandnerdy; 07-14-2010 at 01:22 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by whiteandnerdy
    I'm trying to find out from inside a function how many characters have been allocated in memory to a string array. The catch is that the function only has a pointer to the array that it takes as an argument.
    (...)
    Is there a nice way to get this information from within the function?
    No, at least not one that is portable. Just provide another parameter for the size.
    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
    Jul 2010
    Posts
    2
    Fair enough. Thanks for the quick reply

  4. #4
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Typically you can make a struct like
    Code:
    struct String
    {
        char* data;
        unsigned size;
    }
    and any functions you like to help you.

    Alternatively, you use a terminating character like '\0' c-strings use. So you would have
    Code:
    int get_free_chars(char *ptr)
    {
        while(*ptr != '\0')
           ptr++;
    }
    which does what strlen() does in this matter for a c-string. But I guess you want the first option which is faster in terms of performance

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner: Linked List question
    By WeatherMan in forum C++ Programming
    Replies: 2
    Last Post: 04-03-2008, 07:16 AM
  2. Quick IF statement question (beginner)
    By jim.rattlehead in forum C Programming
    Replies: 23
    Last Post: 11-29-2007, 06:51 AM
  3. beginner question
    By Barrot in forum C++ Programming
    Replies: 4
    Last Post: 08-19-2005, 02:17 PM
  4. Question About External Files (Beginner)
    By jamez05 in forum C Programming
    Replies: 0
    Last Post: 08-11-2005, 07:05 AM
  5. Beginner on Win32 apps, lame question.
    By Templario in forum C Programming
    Replies: 3
    Last Post: 11-06-2002, 08:39 PM