|
-
Jun 7th, 2004, 09:36 AM
#1
Thread Starter
Member
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
-
Jun 7th, 2004, 10:27 AM
#2
Fanatic Member
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.
-
Jun 7th, 2004, 11:35 AM
#3
Monday Morning Lunatic
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
-
Jun 7th, 2004, 03:15 PM
#4
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.
-
Jun 7th, 2004, 05:55 PM
#5
Monday Morning Lunatic
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
-
Jun 7th, 2004, 07:49 PM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|