C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
Iron Speed
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
Ajax Application Generator Generate database and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!
  #1  
Old May 4th, 2008, 01:07 PM
NBDu NBDu is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2008
Posts: 1 NBDu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 11 m 50 sec
Reputation Power: 0
Exclamation Need help w/ using scalar variables

I am trying to reverse the characters in a word array using pointers. I have been able to print the characters in the word array using pointers, but I can not get the pointer to point to the last chartacter in the word first. Then move the pointer to the second to last character and so on. Can you explain to me how I can get the pointer to point to the last charter of the array? Or direct me to a web page that can help me to better understand pointers?

Reply With Quote
  #2  
Old May 7th, 2008, 03:57 AM
MaHuJa MaHuJa is offline
Contributing User
Click here for more information.
 
Join Date: Dec 2007
Posts: 338 MaHuJa User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 14 h 13 m 10 sec
Reputation Power: 1
Send a message via Skype to MaHuJa Send a message via XFire to MaHuJa
Pointers are the bane of many a programmer wannabe

As far as the CPU is concerned, a pointer (to anything) is an integer like any other integer, except that it is used only to keep track of where something else is. An index into that huge array we call memory in normal speech.


pai() meaning PrintAsInt() - how to define this will depend on wether we are working in C or in C++.

Code:
int main() {
  char* alphabet = "abcde";
  int i=3;
  int* ip=&i;   // ip = address_of i

  pai(alphabet);  // A number for a position in memory. 
  pai(i);  // 3
  pai(ip); // A number for the place in memory where i is.

  pai (*ip);  // 3

}


If we pretend we did have an array "memory" for representing the whole of the memory for the computer (program), that last line would essentially be
pai(memory[ip]);


Does that help?


You can't convert between integers and pointers easily though, and that is for several reasons. Most importantly, the size of a pointer and the size of an int may be different. And pointers are treated separately because memory locations aren't "useful" for many other purposes. And hardcoding a pointer address is an extremely bad idea.

-----

The code below may contain other things you don't know, feel free to ask. I didn't test it, so I may have made some error.
Code:
void reverse( char* begin, int len = -1 ) {
  if ( -1==len ) len = strlen(begin);
  char* end = &begin[len];
  while (len>1) {
    char c = *begin;   // swap the chars pointed to by begin and end
    *begin++ = *end;  // and increase end - 
    *end-- = c;
    len-=2;
  }
}

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Need help w/ using scalar variables


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 

Iron Speed




© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway