Thread: char and others different???

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    36

    char and others different???

    if i type in the following code i know that p will output the memory address of the first element, *p will output the value of the first element,p[a] will output the value of the element in b with the index number a and &p[a] will output the memory address of the same...

    and when i typed this in i was right:
    Code:
    #include <iostream>
    using namespace std;
    
    
    int main(){
    
    
        int *p;
        int b[10] = {1234,12,351,25,235,123,5,235,1235,000};
        p = b;
        for(int a = 0; a < 10; a++){
    
    
            cout << p << "\t" << *p << "\t" << p[a] << &p[a] << endl;
    
    
        }
    }
    but when i try the same code with char datatype it doesn't work out well:
    Code:
    #include <iostream>
    using namespace std;
    
    
    int main(){
    
    
        char *p;
        char b[5] = {'a','a','z','a','d'};
        p = b;
        for(int a = 0; a < 10; a++){
    
    
            cout << p << "\t" << *p << "\t" << p[a] << &p[a] << endl;
    
    
        }
    }
    please friends out there, help me...
    Last edited by tennisstar; 11-02-2012 at 07:32 AM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Your problem has nothing do do with changing type from int to char. It is related to the fact you have reduced the number of elements in the array, but are still printing out the same number of elements.

    What do you expect to happen when you access and print the 9th element of a 5-element array?
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    In the loop you are running until 10 ,not five.So it will output garbage.
    If I set a char pointer to a string "samaras" and i output that pointer it will print samaras
    If I set a char pointer to the very same string,but in the letter m for example,then it will output maras

  4. #4
    Registered User
    Join Date
    Jul 2012
    Posts
    36

    Exclamation i dont mean that!

    Quote Originally Posted by grumpy View Post
    Your problem has nothing do do with changing type from int to char. It is related to the fact you have reduced the number of elements in the array, but are still printing out the same number of elements.

    What do you expect to happen when you access and print the 9th element of a 5-element array?
    hey grumpy,
    i didnt mean that...
    it was quite a misunderstanding...
    i know the loop fault and that is not my "big trouble"..
    that was just a small mistake which i made
    what is meant and what exactly my "big trouble" is:
    Char:
    p = prints out the entire array.
    Int:
    p = prints memory address of the first element
    Char:
    *p = prints out the first element's value
    Int:
    *p = prints out the first element's value
    Char:
    p[a] = prints out the value of the element in b whoz index number is equal to a
    Int:
    p[a] = prints out the value of the element in b whoz index number is equal to a
    Char:
    &p[a]: prints out the entire string starting form a, and so how to get the mem. address of a particular element???
    Int:
    &p[a] = prints out the mem. address of the element in b whoz index number is equal to a

    so i'm asking why are these differences between char and other datatypes...

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    When "cout" is told to print a char pointer( char* ), it assumes that you meant to print the null-terminated string that this address is pointing to, not the address itself.
    Devoted my life to programming...

  6. #6
    Registered User
    Join Date
    Jul 2012
    Posts
    36

    Thanx a lot GReaper... but i have a bit more doubts, which arose due to your reply...

    Quote Originally Posted by GReaper View Post
    When "cout" is told to print a char pointer( char* ), it assumes that you meant to print the null-terminated string that this address is pointing to, not the address itself.

    hey GReaper,

    you told that cout prints out assuming that the user meant to print the whole string so what to do when i want the address of the first element? i'll be grateful to you once you clear that doubt of mine and one last thing how to get the memory address of a particular element which in case of int arrays is done something like:
    Code:
    int *ptr;
    int a[10] = {1,2,3,4,5,6,7,8,9,10};
    cout << &ptr[4];
    //this should output the memory address of the element containing 5

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    In the case of a char*, if you want to print the address of a particular element you should cast to a void pointer:
    Code:
    char *p;
    char b[5] = {'a','a','z','a','d'};
    p = b;
    
    cout << reinterpret_cast<void*>(&p[4]);
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 07-24-2012, 10:41 AM
  2. undefined reference to `RunSwmmDll(char*, char*, char*)'
    By amkabaasha in forum C++ Programming
    Replies: 1
    Last Post: 10-31-2011, 12:33 PM
  3. Replies: 8
    Last Post: 12-08-2009, 02:47 AM
  4. Read File To Char Array with Null char init
    By MicroFiend in forum Windows Programming
    Replies: 1
    Last Post: 10-28-2003, 06:18 PM
  5. Assigning Const Char*s, Char*s, and Char[]s to wach other
    By Inquirer in forum Linux Programming
    Replies: 1
    Last Post: 04-29-2003, 10:52 PM