Thread: Pointer to long long

  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    8

    Pointer to long long

    I am making a function that will return a pointer to a long long variable. For example, I have the next variable prototype:
    Code:
    long long funcName(long long x, int s);
    I want to change the return value, and the first parameter to pointers to long long.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Code:
    long long *funcName(long long *x, int s)
    But if you don't even know the most basic pointer syntax, you are almost certainly going to misuse the pointers themselves, and cause some sort of difficult-to-debug problem that may result in program crashes and strange behavior. I strongly recommend reading up on pointers in your C textbook, or on some good online tutorials (like the tutorial on this site, for example).

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    By "change the return value", you may not need the function to return a pointer nor to accept a pointer. The caller can do that.

    Code:
         long long retval;
    
         retval = funcName(2LL, 3);    /* call consistent with the OP's original code */
    
         retval += 42;     /*  we can change retval here */

    You only need the function to work with pointers if the function needs to change variables passed by the caller.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Nov 2013
    Posts
    8
    So, if I use it on malloc(), would it be:
    Code:
    malloc(*(long long));
    ?
    But that gives me the error, "error: expected expression before ')' token"

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    No, it would be:
    Code:
    x = malloc(sizeof(*x));
    You allocate space for what the pointer points to, not the pointer itself (which already exists anyway).
    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. Generate unsigned long long type random numbers
    By patishi in forum C Programming
    Replies: 27
    Last Post: 09-11-2013, 09:03 PM
  2. Replies: 1
    Last Post: 04-23-2011, 08:40 PM
  3. Replies: 1
    Last Post: 10-11-2010, 01:53 AM
  4. Conver long to 8 byte array and back to long
    By plopes in forum C Programming
    Replies: 3
    Last Post: 04-01-2009, 12:39 AM
  5. STLport with MingW - Long Long error on project build
    By Mario F. in forum C++ Programming
    Replies: 2
    Last Post: 08-21-2006, 08:55 AM

Tags for this Thread