CodeChef is a non-commercial competitive programming community
Login
Username (New User? Signup) Password (Forgot Password?)
Signup
Login or
Signup with
Connect
Note
  • Publicize your achievements on your Facebook Wall.
  • Challenge your friends or ask them for help.

Site Navigation

  • PRACTICE
    • Easy
    • Medium
    • Hard
    • Challenge
    • Peer
  • COMPETE
    • February Long Contest
    • January CookOff
    • January Long Contest
  • DISCUSS
    • Wiki
    • Forums
    • Blog
    • Twitter
  • COMMUNITY
    • CodeChef Meetups
    • Campus Chapters
    • Host your Contest
    • User Groups
    • CodeChef TechTalks
    • All Educational Initiatives
    • Event Calendar
  • HELP
    • Frequently Asked Questions
    • FAQ for problem setters
    • Problem Setting
    • Ranks
    • Tutorials
  • ABOUT
    • About CodeChef
    • Team CodeChef
    • Press Room
    • CodeChef Financials
    • CodeChef Sponsorships
    • CEO's Corner
    • Contact Us
    • About Directi
Home » Wiki »  (1) C\ C++.

(1) C\ C++.

[1] Pointer Alises.


This small article shows how error might occurs when one pointer is used in terms of another pointer.
Mainly this article shows how reference to pointer can be used.

Content of pointer [i.e address] can be used in many ways, some on which are as follows :

[A] Content of one pointer, say 'a' can be copied into another pointer, say 'b' and
then pointer 'a' can be used in terms of pointer 'b'.


Example code:

[code]

void showdata(int *b)
{
printf("%d %d", b[0], b[1]);
}

void engine()
{
int *a = new int [2];
a[0] = 10;
a[1] = 20;
showdata(a);   
}

Output:
10 20

[/code]

OK that's as simple as anything !!!

[B] Now, we can allocate memory in some entity and use that memory in some other entity.
Accessing the same actual pointer in between entities or functions.

Example code:

[code]

void engine(int *b) 
{
b = new int[2];
b[0] = 10;
b[1] = 20;   
}

void showdata()
{
int *a = NULL;
engine(a); //memory allocation\ initialization procedure
printf("%d %d", a[0], a[1]);
}

Output:
Runtime Error !!! [crash]

[/code]

Here output 10 20 might be expected, but it's not the case.
Note :
(i)while a is passed to engine() :  a = b = NULL
(ii)in procedure engine() memory is allocated for pointer 'b' and not for pointer 'a'.
Therefore, when we return from engine(), 'a' is still equal to NULL and b was pointing to newly allocated memory.

Solution: Collect the pointer by reference.

Example code:

[code]

void engine(int* &b) 
{
b = new int[2];
b[0] = 10;
b[1] = 20;   
}

void showdata()
{
int *a = NULL;
engine(a); //memory allocation\ initialization procedure
printf("%d %d", a[0], a[1]);
}

Output:

10 20   //as intended[/code]


So, Pointer Alises can be used in two ways:
[1]Content based : accessing, the address that is pointed to by, the actual pointer through an alise.
[2]Address based : accessing, the address, of actual pointer through an alise.

 

 


Comments

  • Login or Register to post a comment.

put some more challenging

vish @ 14 Nov 2009 12:23 PM

put some more challenging ............

CodeChef is a non-commercial competitive programming community
  • About CodeChef
  • About Directi
  • CEO's Corner
  • C-Programming
  • Programming Languages
  • Contact Us
© 2009 Directi Group. All Rights Reserved. CodeChef uses SPOJ © by Sphere Research Labs
In order to report copyright violations of any kind, send in an email to copyright@codechef.com
CodeChef a product of Directi
The time now is:
CodeChef - A Platform for Aspiring Programmers

CodeChef was created as a platform to help programmers make it big in the world of computer programming. At CodeChef we work hard to revive the geek in you by hosting programming contests on a monthly basis. We also aim to have training sessions and events related to online programming for programmers around the world. Apart from providing a platform for programming competitions, CodeChef also has various tutorials and forum discussions to help those who are new to the world of computer programming.

Practice Section - A Place to hone your 'Computer Programming Skills'

Try your hand at one of our many practice problems and submit your solution in a language of your choice. Our judge accepts solutions in over 35+ programming languages. Online programming was never this much fun! Receive points, and move up through the CodeChef ranks. Use our practice section to better prepare yourself for the multiple programming competitions that take place through-out the month on CodeChef.

Compete - Monthly Programming Contests and Cook-offs

Here is where you can show off your computer programming skills. Take part in our 10 day long monthly programming contests and the shorter format Cook-off programming contests. Put yourself up for recognition and win great prizes. Prizes worth up to Rs.20,000 and $700 are up for grabs every month along with lots more CodeChef goodies.

Discuss

Are you new to computer programming? Do you need help with algorithms? Then be part of CodeChefs Forums and interact with all our programmers love helping out other programmers and share their ideas.

CodeChef Community

As part of our Educational initiative, we give institutes the opportunity to associate with CodeChef in the form of Campus Chapters. Hosting online programming competitions is not the only feature on CodeChef. Be a part of the CodeChef community through CodeChef meetups and techtalks. You can also host a programming contest for your institute on CodeChef and be a guest author on our blog.

Domain Name Registration, Web hosting, and Website Design provided by BigRock.com