-
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
-
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.
-
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]);}
-
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;
}
-
Look carefully. That's actually not part of the compiler, it's the preprocessor. The compiler input parsing rules don't apply.
-
Quote:
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.