S
sieg1974
Hi,
I have made this simple program to understand char ** pointers, but I
still having many questions.
int main()
{
char ** testPointerPointerChar = 0;
char * A = "string01";
char * B = "string02";
if ( ( testPointerPointerChar = malloc( 50 * 256 * sizeof( char ) ) )
== NULL )
return( -1 );
/*
*( testPointerPointerChar + 0 ) = A;
*( testPointerPointerChar + 1 ) = B;
*/
strcpy( *( testPointerPointerChar + 0 ), A );
strcpy( *( testPointerPointerChar + 1 ), B );
printf( "%s\n", *( testPointerPointerChar + 0 ) );
printf( "%s\n", *( testPointerPointerChar + 1 ) );
return( 0 );
}
1. testPointerPointerChar pointer points to a matriz of pointers, and
each one of them points to a char. Right?
2. Is testPointerPointerChar = malloc(...) allocating memory for this
matriz of pointers or for the block of chars?
3. With malloc( 50 * 256 * sizeof( char ) ) ) am I allocating memory
space for 50 string of 256 characters. Right?
4. *( testPointerPointerChar + 1 ) = B seems to work fine, making the
second pointer to point to B, but why strcpy( *(
testPointerPointerChar + 1 ), B ) doesn't work? It generates a
segmentation fault when this program is executed.
5. If I need to allocated memory space for the block of 50 pointers to
char, how can I do it? Would malloc( 50 * sizeof( int ) ) work? And is
the memory used by a pointer the same as an integer?
Thanks for all the help,
Andre
I have made this simple program to understand char ** pointers, but I
still having many questions.
int main()
{
char ** testPointerPointerChar = 0;
char * A = "string01";
char * B = "string02";
if ( ( testPointerPointerChar = malloc( 50 * 256 * sizeof( char ) ) )
== NULL )
return( -1 );
/*
*( testPointerPointerChar + 0 ) = A;
*( testPointerPointerChar + 1 ) = B;
*/
strcpy( *( testPointerPointerChar + 0 ), A );
strcpy( *( testPointerPointerChar + 1 ), B );
printf( "%s\n", *( testPointerPointerChar + 0 ) );
printf( "%s\n", *( testPointerPointerChar + 1 ) );
return( 0 );
}
1. testPointerPointerChar pointer points to a matriz of pointers, and
each one of them points to a char. Right?
2. Is testPointerPointerChar = malloc(...) allocating memory for this
matriz of pointers or for the block of chars?
3. With malloc( 50 * 256 * sizeof( char ) ) ) am I allocating memory
space for 50 string of 256 characters. Right?
4. *( testPointerPointerChar + 1 ) = B seems to work fine, making the
second pointer to point to B, but why strcpy( *(
testPointerPointerChar + 1 ), B ) doesn't work? It generates a
segmentation fault when this program is executed.
5. If I need to allocated memory space for the block of 50 pointers to
char, how can I do it? Would malloc( 50 * sizeof( int ) ) work? And is
the memory used by a pointer the same as an integer?
Thanks for all the help,
Andre