| | |
Question for Narue.
![]() |
Hi Narue,
Ive started your tutorial on pointers and had a few questions about it if you don't mind.
1) You talked about the fact that a function has an adres itself and that you can call a function threw it's adres. This I understand, as I tried it out wether I could print the adres of a function a did this as follow:
This worked as it should, thing is, I tried this out aswell with &function and *function and those two gave me the exact adres.
Question is, are all three correct possibilities to obtain the functions adres?
2) I tried out the example you showed about pointers to functions and had a problem that I got two errors, they stated the following:
wich is related to this piece of code:
Now, I understand what the error message is saying and I changed the code so that the parameter const void *a, get's cast like this:
This way it works, but, I kept wondering wether this was not related towards either the compiler (MS VC 6.00) I'm using or some other reason
Anyway, thanks for the superb tutorial, Ive allready learned alot
Ive started your tutorial on pointers and had a few questions about it if you don't mind.
1) You talked about the fact that a function has an adres itself and that you can call a function threw it's adres. This I understand, as I tried it out wether I could print the adres of a function a did this as follow:
C Syntax (Toggle Plain Text)
void function(int, int) { ... } int main() { ... cout<< function <<endl; ... }
This worked as it should, thing is, I tried this out aswell with &function and *function and those two gave me the exact adres.
Question is, are all three correct possibilities to obtain the functions adres?
2) I tried out the example you showed about pointers to functions and had a problem that I got two errors, they stated the following:
C Syntax (Toggle Plain Text)
C:\Program Files\Microsoft Visual Studio\MyProjects\VoorbeeldenInternet\PointersTutorialByNarue\Testing.cpp(39) : error C2440: 'initializing' : cannot convert from 'const void *' to 'const int *' Conversion from 'void*' to pointer to non-'void' requires an explicit cast
wich is related to this piece of code:
C Syntax (Toggle Plain Text)
int compare ( const void *a, const void *b ) { const int *ia = a; const int *ib = b; ... }
Now, I understand what the error message is saying and I changed the code so that the parameter const void *a, get's cast like this:
C Syntax (Toggle Plain Text)
const int *ia = static_cast <const int*> (a);
This way it works, but, I kept wondering wether this was not related towards either the compiler (MS VC 6.00) I'm using or some other reason

Anyway, thanks for the superb tutorial, Ive allready learned alot
>are all three correct possibilities to obtain the functions adres?
You can only do two things with a function: call it, and take its address. If you're not calling it, then the compiler assumes you're taking its address.
>I tried out the example you showed about pointers to functions and had a problem that I got two errors
The example (and all examples unless otherwise stated) was written in C. You've stumbled across one of the differences between C and C++, where C++ requires an explicit cast when converting back from a pointer to void. C performs this conversion implicitly.
You can only do two things with a function: call it, and take its address. If you're not calling it, then the compiler assumes you're taking its address.
>I tried out the example you showed about pointers to functions and had a problem that I got two errors
The example (and all examples unless otherwise stated) was written in C. You've stumbled across one of the differences between C and C++, where C++ requires an explicit cast when converting back from a pointer to void. C performs this conversion implicitly.
I'm here to prove you wrong.
Ok, thanks Narue :!:
Ive got a few more if you don't mind
Ive read the part about incrementing and subtracting pointers, but I don't see the difference in the examples you show for those two, they're actually both the same
Also, I don't understand what the idea of these are, meaning, when would you use them?
The example:
Also, when I execute the two examples for incrementing and subtracting, they both give me 'td' as result
But, when executing the one in wich you cast the result to (long) (atleast I think it's a cast
) I get a result of -5 and 5. The same result is shown when I use cout?
Is this again related towards being C code?
The results of -5 and 5, are those the amount of pointers that have been used, or what are they?
Thanks for the help and explanation!
Ive got a few more if you don't mind
Ive read the part about incrementing and subtracting pointers, but I don't see the difference in the examples you show for those two, they're actually both the same
Also, I don't understand what the idea of these are, meaning, when would you use them?
The example:
C Syntax (Toggle Plain Text)
#include <stdio.h> int main ( void ) { int a[] = {1,2,3,4,5}; int *p = a, *q = a + 5; printf ( "%td\n", p - q ); printf ( "%td\n", q - p ); return 0; }
Also, when I execute the two examples for incrementing and subtracting, they both give me 'td' as result
But, when executing the one in wich you cast the result to (long) (atleast I think it's a cast
) I get a result of -5 and 5. The same result is shown when I use cout?Is this again related towards being C code?
The results of -5 and 5, are those the amount of pointers that have been used, or what are they?
Thanks for the help and explanation!
>when would you use them?
It's an easy way to determine the distance between two pointers. You use them when you need just that, but in general you won't need that feature often.
>they both give me 'td' as result
Your compiler doesn't support C99.
>I get a result of -5 and 5. The same result is shown when I use cout?
That's the correct result, and cout would give you the same thing.
>The results of -5 and 5, are those the amount of pointers that have been used, or what are they?
It's the distance between the two pointers. The equivalent with indices is saying 5 - 0 and 0 - 5.
It's an easy way to determine the distance between two pointers. You use them when you need just that, but in general you won't need that feature often.
>they both give me 'td' as result
Your compiler doesn't support C99.

>I get a result of -5 and 5. The same result is shown when I use cout?
That's the correct result, and cout would give you the same thing.
>The results of -5 and 5, are those the amount of pointers that have been used, or what are they?
It's the distance between the two pointers. The equivalent with indices is saying 5 - 0 and 0 - 5.
I'm here to prove you wrong.
Hi Narue,
Ive tried to alter your example:
into this:
I'm getting an error though wich seems to be related towards deleting p
What am I doing wrong there?
Could you tell me also how I can cast the C example so I can execute your example.
Thanks.
Ive tried to alter your example:
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> #include <string.h> char *reverse ( const char *s ) { int i = 0, j = strlen ( s ); char *save = malloc ( j + 1 ); if ( save == NULL ) return NULL; while ( j > 0 ) save[i++] = s[--j]; save[i] = '\0'; return save; } int main ( void ) { char *p; p = reverse ( "J. Random Guy" ); puts ( p ); free ( p ); return 0; }
into this:
C Syntax (Toggle Plain Text)
#include <iostream> using namespace std; char *reverse ( const char *s ) { int i = 0, j = strlen ( s ); char *save = new (char) ( j + 1 ); if ( save == NULL ) return NULL; while ( j > 0 ) save[i++] = s[--j]; save[i] = '\0'; return save; } int main ( void ) { char *p; p = reverse ( "J. Random Guy" ); cout<< p <<endl; delete p; return 0; }
I'm getting an error though wich seems to be related towards deleting p

What am I doing wrong there?
Could you tell me also how I can cast the C example
C Syntax (Toggle Plain Text)
char *save = malloc ( j + 1 );
Thanks.
>new (char) ( j + 1 );
Beware that new is very flexible. This is a valid expression, but it doesn't do what you think it does. To allocate more than a single char, use new[]:
And then be sure to use the corresponding delete[]:
>What am I doing wrong there?
You're only allocating space for a single char, then initializing its value to j + 1. The result is that the subscripting done later trashes memory that you don't own, which is probably used for the memory manager's bookkeeping, and when you try to release the memory, the memory manager panics because it doesn't find what it's looking for.
>Could you tell me also how I can cast the C example
The easiest way is to just use a C-style cast:
Beware that new is very flexible. This is a valid expression, but it doesn't do what you think it does. To allocate more than a single char, use new[]:
C Syntax (Toggle Plain Text)
char *save = new char[j + 1];
C Syntax (Toggle Plain Text)
delete [] p;
You're only allocating space for a single char, then initializing its value to j + 1. The result is that the subscripting done later trashes memory that you don't own, which is probably used for the memory manager's bookkeeping, and when you try to release the memory, the memory manager panics because it doesn't find what it's looking for.
>Could you tell me also how I can cast the C example
The easiest way is to just use a C-style cast:
C Syntax (Toggle Plain Text)
char *save = (char*)malloc ( j + 1 );
I'm here to prove you wrong.
Hey Narue,
Your tutorial on 'Pointers and dynamic memory'.
I executed your example in wich you use this piece of code
Since I wanted to see how I had to write this in C++, I tried it out this way
Though it worked, I wanted to ask wether, this is the correct way?
Your tutorial on 'Pointers and dynamic memory'.
I executed your example in wich you use this piece of code
C Syntax (Toggle Plain Text)
char *p = malloc ( sizeof "This is a test" );
Since I wanted to see how I had to write this in C++, I tried it out this way
C Syntax (Toggle Plain Text)
char *p = new char[sizeof "This is a test"];
Though it worked, I wanted to ask wether, this is the correct way?
•
•
•
•
Originally Posted by JoBe
Hey Narue,
Your tutorial on 'Pointers and dynamic memory'.
I executed your example in wich you use this piece of codeC Syntax (Toggle Plain Text)
char *p = malloc ( sizeof "This is a test" );
Since I wanted to see how I had to write this in C++, I tried it out this wayC Syntax (Toggle Plain Text)
char *p = new char[sizeof "This is a test"];
Though it worked, I wanted to ask wether, this is the correct way?
C code in your example:
C Syntax (Toggle Plain Text)
base = malloc ( 2 * 3 * sizeof *base ); p = malloc ( 2 * sizeof *p );
Altering this into C++ code, is this correct?
C Syntax (Toggle Plain Text)
base = new int[2]; p = new int*[3];

Also, how do I delete this double array?
Like this:
C Syntax (Toggle Plain Text)
delete []base; for (i = 0; i < 3; i++) delete [i] p;
I think I got the solution to define, initialize and delete the two dimensional array with pointers in C++ Narue.
Correct
Also, to free the memory in the double array example written in C, is this correct then:
#include <iostream>
using namespace std;
int main ()
{
int **p;
int i, j;
p = new int*[2];
for ( i = 0; i < 2; i++ )
{
p[i] = new int[3];
for ( j = 0; j < 3; j++ )
*( *( p + i ) + j ) = i * j;
}
for ( i = 0; i < 2; i++ )
{
for ( j = 0; j < 3; j++ )
cout<< p[i][j];
cout<<endl;
}
for (i = 0; i < 2; i++)
delete [] p[i];
delete [] p;
return 0;
}Correct

Also, to free the memory in the double array example written in C, is this correct then:
C Syntax (Toggle Plain Text)
free (base); free (p);
![]() |
Similar Threads
- Help needed :( (C++)
- i need login code in C# (C#)
Other Threads in the C Forum
- Previous Thread: HTTP protocol help
- Next Thread: Help for twodimentional arrays
| Thread Tools | Search this Thread |
* adobe ansi api array arrays binarysearch calculate centimeter char character cm convert copyanyfile copypdffile createcopyoffile createprocess() csyntax directory dynamic fflush file floatingpointvalidation fork forloop frequency getlasterror getlogicaldrivestrin givemetehcodez global graphics gtkgcurlcompiling gtkwinlinux hardware highest homework i/o ide inches intmain() iso km license linked linkedlist linux linuxsegmentationfault list logical_drives loopinsideloop. lowest match matrix microsoft motherboard mqqueue mysql oddnumber odf open opendocumentformat opensource openwebfoundation pattern pdf performance pointer posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprogramming stack standard strchr string suggestions test unix urboc user variable voidmain() whythiscodecausesegmentationfault win32api windows.h windowsapi






