LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-22-2007, 03:08 AM   #1
MegaFighterX
LQ Newbie
 
Registered: Feb 2007
Posts: 11

Rep: Reputation: Disabled
c++ pointer problems


I'm sure there's a few million threads on these.... and I'm sorry for bugging you folks again.

Here's the deal. I've got two arrays that are going to be interchanging. But it's specified I have to use pointers to swap them instead of doing it the easy way and using a temp variable to do the swap. Here's what I've got...

Code:
    int one[(arbitraty number]; 
    int two[(same arbitrary number)];
    int* addrhldr;
//For first element swap. 
    addrhldr = &one[0];
    &one[0] = &two[0];
    &two[0] = addrhldr;
It's coming up with errors for the last two lines:
non-lvalue in assignment. I pulled it off somewhat like this once, but then I screwed with the code and now I want to bash my head into something because of this... Any help's appreciated.
 
Old 02-22-2007, 05:47 AM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 30
Your compiler is trying to tell you that what you're doing doesn't make sense. It doesn't make sense to me either. (grin)

one is an array of integers. So is two. They're arrays of integers, not arrays of pointers. The only place you can store a pointer in this code, with the variables as they're declared now, is in addrhldr.

So how do you expect to swap pointers? You have only one pointer in the whole program.

If someone is specifying such a low-level detail that you have to swap pointers, then I'm assuming this is a class assignment. If that's the case, then fixing this problem (unless you deeply understand the fix) is not going to help you in the long run. You may wish to consult your instructor for some help on underlying concepts.

Hope this helps.

Last edited by wjevans_7d1@yahoo.co; 02-22-2007 at 05:49 AM.
 
Old 02-22-2007, 06:15 AM   #3
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Sounds like a class assignment so...
How do you get the address of the first element of an array? (hint the answer is the same for any array)
Once you have answered that then you well earned hint is:

You need the address of the first element of both arrays, hence at least two pointers are required...

Last edited by graemef; 02-22-2007 at 06:23 AM.
 
Old 02-22-2007, 08:09 AM   #4
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Have a look at std::iter_swap I think it is in the header algorithm, using this an graemef's hint is the key. You do not need to user the std function but you will be just reinventing the wheel if you don't.

Last edited by dmail; 02-22-2007 at 08:11 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
pointer problems cyb0rg777 Programming 10 03-31-2006 11:31 AM
returning data to main() via a pointer to a pointer. slzckboy Programming 3 05-30-2005 01:20 PM
hot to set value of pointer to pointer to a structure in C alix123 Programming 2 11-17-2004 06:40 AM
linux mouse pointer problems najeeb_a_99 Linux - Newbie 0 10-06-2001 04:14 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:40 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration