string comparision : Urgent problem

This is a discussion on string comparision : Urgent problem within the C Programming forums, part of the General Programming Boards category; Originally Posted by moonlord there is a convention that says: if x is a pointer to something, or an array ...

  1. #16
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by moonlord
    there is a convention that says: if x is a pointer to something, or an array of that kind, then &a[0] == a
    There's even a better one that says:

    "If a function takes a given type, you can actually just pass it that given type without convoluting the hell out of it."

    The function took a pointer to a character. So passing it one is correct. Passing some horribly ugly dereferenced / address of / dereferenced / address of mess doesn't make it any better. It just makes it harder to understand. Consider:
    Code:
    void foo( char * bar);
    This function wants a pointer to a character. Thus, these are both correct:
    Code:
    char c;
    char *p;
    
    foo( &c ); /* the address of a character is a pointer to it */
    foo( p ); /* a pointer to a character, since that's what it wants */
    Oh, sure you could make it ugly as hell and still make it "correct":
    Code:
    char c;
    char *p;
    
    foo( &(*(&(*(&c)))) );
    foo( &0[&*p] );
    There's just no point in doing so.


    Quzah.
    Hope is the first step on the road to disappointment.

  2. #17
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,167
    Quote Originally Posted by ILoveVectors
    char sentence[30] = "whatever"; // orginal data
    char *psentence = &sentence; // pointer to address of original data
    Actually, the second line should just be:
    char *psentence = sentence;

    Remember that the name of an array by itself is actually a pointer to the first element of the array.
    If you understand what you're doing, you're not learning anything.

  3. #18
    Banned
    Join Date
    Jun 2005
    Posts
    594
    oops my mistake, i have to stop making post
    when im half asleep.

    yea and plus i most do c++ so i was prolly thinking this

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
    	string sentence2 = "whatever2";
    	string *psentence2 = &sentence2;
    
    	cout << sentence2 << endl;
    	cout << *psentence2 << endl;
    	cin.get();
    	return 0;
    }
    Last edited by ILoveVectors; 08-08-2005 at 07:47 PM.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 03:13 PM
  2. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 02:40 AM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  4. Replies: 5
    Last Post: 11-07-2005, 10:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21