Pointer lesson needed

This is a discussion on Pointer lesson needed within the C++ Programming forums, part of the General Programming Boards category; Sorry in advance. I need a pointer class. I'm working on an overload + operator that takes and returns the ...

  1. #1
    Registered User verbity's Avatar
    Join Date
    Nov 2006
    Posts
    101

    Pointer lesson needed

    Sorry in advance. I need a pointer class. I'm working on an overload + operator that takes and returns the char in a String at *(s + x) where s is a String and x is an int...so s[x]...

    I have it set up as a friend and the following:

    Code:
    char* operator+(const String& s, int x)
    {
    	return s.m_buf[x];
    }
    ......obviously that's wrong but if I put &s.m_buf[x] wouldn't that double reference?? So bottom line is I don't friggin' know what to put there for a return value and Bjarne Stroupstrup needs to write a book idiots like me can read cause I can't understand a thing he says....LOL!!! Any help or better yet a nice little explaination would be cool.

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,622
    s.m_buf[x] is like *(s.m_buf + x), accessing the character x in s.m_buff
    so, following that, &s.m_buf[x] takes the address of x in s.m_buff
    since pointers hold the address of an object, it's the Right Thing

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    135
    If you just want to return a char, then just return a char and not a pointer to a char.

    Rewrite your return as: char operator + (blah.. blah.. blah..);
    else, what you're doing when you apply the ampersand to the variable m_buf is you're returning the address of the current index, x.

    And no,that would not be double dereferencing. dereferencing would be: char* ip; *ip = 'v'; dereferencing is applied on pointers. none of the variables in your code are pointers

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    You can treat a reference just as if it was another name, an alias, for the original variable.
    Code:
    int var = 1, &ref = var;
    int *p1 = &var, *p2 = &ref;
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 12-25-2007, 04:01 AM
  2. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  3. Another Linked List plee
    By Dragoncaster131 in forum C Programming
    Replies: 3
    Last Post: 05-15-2004, 05:40 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. file pointer
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-11-2001, 03:52 PM

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