l-value? (Pointer)

This is a discussion on l-value? (Pointer) within the C++ Programming forums, part of the General Programming Boards category; Please help me resolve this. What did I do wrong? Code: char *findC (char const *source, char const *obj) { ...

  1. #1
    843
    843 is offline
    Registered User
    Join Date
    Oct 2010
    Posts
    135

    l-value? (Pointer)

    Please help me resolve this. What did I do wrong?

    Code:
    char *findC (char const *source, char const *obj)
    {
        int i;
    
        for (i = 0; i < 6; i++) {
            if (*(source + i) == *obj) {
                return &(source + i); //l-value error here
            }
        };
    
        return NULL;
    }

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    90
    You can't take the address of an expression, only of a variable. Even if you could, you'd be returning a char** instead of a char*. Get rid of the & and you should be fine.

  3. #3
    843
    843 is offline
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    I got this error when the & is removed:

    error C2440: 'return' : cannot convert from 'const char *' to 'char *'|

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    90
    Right. Since source is a const char *, and the return value is based on that, you need to return a const char *.

  5. #5
    843
    843 is offline
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    How do I do that? It keeps giving me a syntax error. Do I need to introduce a new const pointer?

    EDIT: Nope, doesn't work. I have no idea what to do.

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    90
    The return value of your function is "char *". Change it to "const char *".

  7. #7
    843
    843 is offline
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    Many thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer to a function pointer
    By @nthony in forum C Programming
    Replies: 3
    Last Post: 05-30-2010, 06:13 PM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 11:10 AM
  3. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 04:47 PM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 04:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM

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