Results 1 to 6 of 6

Thread: Dumb pointer question

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2003
    Posts
    52

    Dumb pointer question

    Got a quick dumb question, What is the difference between the 2, which is a pointer?
    example:

    void convertToUpperCase( char * );

    int main(){
    convertToStringUpperCase(string);
    return 0;}

    void convertToUpperCase( char *sPtr ){
    while ( *sPtr != '\0'){
    if(islower(*sPtr))
    *sPtr = toupper( * sPtr);
    ++sPtr;
    }
    }

    this is an example of a pointer right, in the function convertToUpperCase, it takes
    a single char parameter that points to sPtr

    Now second example:

    int* array = 0;
    int* temp = 0;
    int index;
    array = new int[10];
    temp = new int[20];
    for( index = 0;index<10;index++)
    temp[index] = array[index];
    delete[] array;
    array = temp;
    temp = 0;

    what the heck is this is this are lines 1 and 2 pointers?
    I don't get this???????
    I thought that to signify a pointer you place the * asterisk before the pointer name


    thanks

  2. #2
    Fanatic Member
    Join Date
    Dec 2003
    Posts
    703
    int* array

    and

    int *array

    are equivalent as far as the compiler is concerned. The variable "array" is a pointer to an int in both cases.
    an ending

  3. #3
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    C and C++ compilers' parsing stages are tokenising parsers, i.e. they split up the input file into tokens (keywords, operators, etc.). Whitespace is ignored outside of quotes (preprocessor instructions have already gone by this stage).

    As in, these two are identical:
    Code:
    int test;
    const char *wombat = "hello";
    
    test   =     strlen(   wombat   )
    
    ;
    for(int i =
    
    0;
     i <               test; ++i)
    
     {
        cout.put(
    
    
    wombat[i]);}
    and
    Code:
    int test;const char *wombat="hello";test=strlen(wombat);for(int i=0;i<test;++i){cout.put(wombat[i]);}
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  4. #4
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403
    Here's one example of where whitespace is important though:
    Code:
    #include <iostream>
    using namespace std;
    
    // no space between name and arguments
    #define MY_MAX(x, y) ((x) > (y) ? (x) : (y)) 
    // space
    #define MY_MAX2 (x, y)  ((x) > (y) ? (x) : (y))
    int main()
    {
    	cout << MY_MAX(1, 3) << endl;
    	// massive compile errors here!:
    	cout << MY_MAX2(1, 4) << endl;
    	return 0;
    }
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  5. #5
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Look carefully. That's actually not part of the compiler, it's the preprocessor. The compiler input parsing rules don't apply.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  6. #6
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403
    Originally posted by sunburnt
    Here's one example of where whitespace is important though:
    Code:
    #include <iostream>
    using namespace std;
    
    // no space between name and arguments
    #define MY_MAX(x, y) ((x) > (y) ? (x) : (y)) 
    // space
    #define MY_MAX2 (x, y)  ((x) > (y) ? (x) : (y))
    int main()
    {
    	cout << MY_MAX(1, 3) << endl;
    	// massive compile errors here!:
    	cout << MY_MAX2(1, 4) << endl;
    	return 0;
    }
    bah, you're no fun


    even if you are right.
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width