Thread: Character pointer problem.

  1. #1
    Registered User kiss_psycho's Avatar
    Join Date
    Feb 2003
    Posts
    49

    Character pointer problem.

    This is a problem i am having when i am trying to obtain the address of a character type variable and print the address using cout. the printing works fine when i am using generic pointers by typecasting or if i use printf().

    here the second cout statement fails but the fifth is OK.

    Strange....

    Code:
    #include<iostream.h>
    
    int main()
    {
    	void *gp1,*gp2,*gp3;
    	int *ip,i;
    	char *cp,c;
    	float *fp,f;
    
    	i=10;
    	c='a';
    	f=11.11;
    
    	ip=&i;
    	cp=&c;
    	fp=&f;
    
    	gp1=ip;
    	gp2=cp;
    	gp3=fp;
    
    	cout<<endl<<"# IP:-- Integer is "<<*ip<<" at address "<<ip<<endl;
    	cout<<"# CP:-- Character is "<<*cp<<" at address "<<cp<<endl;
    	cout<<"# FP:-- Float is "<<*fp<<" at address "<<fp<<endl;
    
    	cout<<"# GP1:-- Integer is "<<*(int*)gp1<<" at address "<<gp1<<endl;
    	cout<<"# GP2:-- Character is "<<*(char*)gp2<<" at address "<<gp2<<endl;
    	cout<<"# GP3:-- Float is "<<*(float*)gp3<<" at address "<<gp3<<endl;
    
    	*ip=c;
    	*cp=f;
    	*fp=i;
    
    	cout<<endl<<"Now pointing to char by int pointer and vice versa..."<<endl;
    	cout<<endl;
    
    	cout<<"# IP:-- Integer is "<<*(char*)ip<<" at address "<<ip<<endl;
    	cout<<"# CP:-- Character is "<<*(float*)cp<<" at address "<<cp<<endl;
    	cout<<"# FP:-- Float is "<<*(int*)fp<<" at address "<<fp<<endl;
    
    	return 0;
    }

    I am using Turbo C++ 3.0 and Borland C++ 5.0 compilers...
    Definition of Programmer : A red-eyed mumbling mammal capable of conversing with inanimate objects.

    Happy Hunting...
    The Root

  2. #2
    Registered User Ward's Avatar
    Join Date
    Sep 2001
    Location
    Belgium
    Posts
    39

    reply on character pointer problem

    int main(int argc, char* argv[])
    {
    void *gp1,*gp2,*gp3;
    int *ip,i;
    char *cp,c;
    float *fp,f;

    i=10;
    c='a';
    f=11.11;

    ip=&i;
    cp=&c;
    fp=&f;

    gp1=ip;
    gp2=cp;
    gp3=fp;

    cout<<endl<<"# IP:-- Integer is "<<*ip<<" at address "<<ip<<endl;
    cout<<"# CP:-- Character is "<<*cp<<" at address "<<cp<<endl;

    //operator<< of object cout automatically interprets the pointer cp (of type char*) as a character array,
    //because it accepts strings by default.
    //this is why you can type the following simple statement:
    //cout<<"Example";
    //The string "Example" is an array of characters too.
    //You also see al lot of strange looking characters after the 'a' value.
    //the operator<< of cout looks for an end of string '\0' character.
    //And this is not immediatly found after the character 'a'.

    cout<<"# FP:-- Float is "<<*fp<<" at address "<<fp<<endl;

    cout<<"# GP1:-- Integer is "<<*(int*)gp1<<" at address "<<gp1<<endl;
    cout<<"# GP2:-- Character is "<<*(char*)gp2<<" at address "<<gp2<<endl;

    //the above explanation explains why this statements works as desired.
    //since the operator<< of cout does not recognize the type of gp2 as a char*,
    //it prints the value of the pointer.

    cout<<"# GP3:-- Float is "<<*(float*)gp3<<" at address "<<gp3<<endl;

    *ip=c;
    *cp=f;
    *fp=i;

    cout<<endl<<"Now pointing to char by int pointer and vice versa..."<<endl;
    cout<<endl;

    cout<<"# IP:-- Integer is "<<*(char*)ip<<" at address "<<ip<<endl;
    cout<<"# CP:-- Character is "<<*(float*)cp<<" at address "<<cp<<endl;

    //here you have the same interpretion as in the first explanation.

    cout<<"# FP:-- Float is "<<*(int*)fp<<" at address "<<fp<<endl;

    return 0;


    }
    Greetings.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  2. pointer problem or so...
    By TL62 in forum C Programming
    Replies: 19
    Last Post: 01-12-2008, 11:45 PM
  3. pointer problem
    By peterx in forum C Programming
    Replies: 3
    Last Post: 11-11-2005, 02:02 PM
  4. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 02:19 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM