Thread: question about void pointer ,stack ,ellipsis

  1. #1
    Registered User
    Join Date
    Mar 2015
    Posts
    3

    question about void pointer ,stack ,ellipsis

    I have an assumption and I don't know is this true or not:
    If i use int arr[5];
    Code:
    arr[0]="hello";
    the "hello"will store some where in the memory and the adress ofthat will store in the arr[0].
    and I think when i have a function like this
    Code:
    void my_func(const char *string,...)

    and I pass a string(array)to that function then it will store it some where in the memmory and the address of that will store in the stack but when I pass an integer type like 'j' to a function like
    Code:
    void test(char s){}
    the j will store in the in stack directly not the address of it. is my assumption true?
    now I want to call that function
    Code:
    my_func("hi how",45,'c');
    and I want to point the next argument so I do it like this:

    Code:
    void my_func(constchar*string,...)
    {
    void*argptr = format;
        argptr =((int*)argptr +1);
    }
    but argptr points to the where "hi how" stored not to the stack (as my assumtion and I don't know if it's true or not)
    how can I do that?
    Last edited by bki123; 03-18-2015 at 01:11 PM.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Check out this link.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    Registered User
    Join Date
    Mar 2015
    Posts
    3
    Quote Originally Posted by Elkvis View Post
    Check out this link.
    I don't want to (i'm not allowed) to use such macros.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by bki123 View Post
    I don't want to (i'm not allowed) to use such macros.
    If you don't use the standard methods of handling a variable number of arguments, you will have to rely on implementation-defined and/or platform-specific behavior.

    One possibility might be to take the address of the first parameter, rather than the parameter itself, and add the size of the parameter to get the next one. Not all parameters are going to be the same size, so you'll have to know the type you want to extract from the list, and C won't tell you at run time what type the parameters are. In your case, you should probably use your first parameter, like a printf format string, to describe the remaining parameters.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Err... why are you not allowed to use such standard macros for such a standard solution?
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Static arrays and ellipsis
    By renzokuken01 in forum C Programming
    Replies: 18
    Last Post: 12-30-2010, 11:25 PM
  2. Ellipsis va_arg(arglist, mytype)
    By tempster09 in forum C Programming
    Replies: 10
    Last Post: 12-06-2009, 11:32 PM
  3. Casting to pointer to pointer to void
    By Sharke in forum C Programming
    Replies: 13
    Last Post: 05-12-2009, 08:40 PM
  4. Replies: 4
    Last Post: 08-27-2007, 11:51 PM
  5. Dereference pointer to void pointer to member
    By phil in forum C Programming
    Replies: 5
    Last Post: 04-20-2005, 11:54 AM

Tags for this Thread