Call a function that accepts char * as const char *&.

This is a discussion on Call a function that accepts char * as const char *&. within the C++ Programming forums, part of the General Programming Boards category; Can i have something like a reference to pointer that can change the pointer but not the value in the ...

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    13

    Call a function that accepts char * as const char *&.

    Can i have something like a reference to pointer that can change the pointer but not the value in the pointed location.

    Code:
    void fail(const char *& i)
    {
        i++;
    }
    
    
    int main()
    {
        char s[]="something";
        char *j=s;
        fail(j);
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,490
    I just changed it to
    const char *j=s;

    and it does what I expected it to.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    2,351
    the code you posted will increment the pointer itself. following the call to fail() in main(), j will now point to the 'o' in "something." the traditional (C) way of doing this was to pass a pointer to the pointer (char**), but C++ allows a reference to a pointer (but not the other way around), so you can modify the pointer itself without needing to dereference anything.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    But why do you want to do it?
    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.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    2,351
    Quote Originally Posted by Elysia View Post
    But why do you want to do it?
    indeed. it would be simpler to do this:

    Code:
    char* fail(char* ptr)
    {
      return ++ptr;
    }

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    That was not my point. Why the need to pass a char pointer in C++?
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. invalid const char[] and char [] to binary operator+
    By drazenmozart in forum C++ Programming
    Replies: 3
    Last Post: 01-07-2013, 12:52 PM
  2. Replies: 9
    Last Post: 08-09-2011, 12:41 PM
  3. Difference between char *str1 & const char *str2
    By Tigers! in forum C Programming
    Replies: 4
    Last Post: 08-06-2009, 04:04 AM
  4. Replies: 1
    Last Post: 11-16-2008, 02:46 PM
  5. Assigning Const Char*s, Char*s, and Char[]s to wach other
    By Inquirer in forum Linux Programming
    Replies: 1
    Last Post: 04-29-2003, 10:52 PM

Tags for this Thread


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