473,241 Members | 1,839 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,241 software developers and data experts.

char ** questions

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
Nov 14 '05 #1
3 2176
sieg1974 wrote:
Hi,

I have made this simple program to understand char ** pointers, but I
still having many questions.
See if the following version of your program helps. The original is
retained after it, below. Note the parts you left out, in particular.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
char **tPPC = 0;
const char *A = "string01";
const char *B = "string02";
if (!(tPPC = malloc(2 * sizeof *tPPC))) {
printf("failed to allocate space for pointers\n");
return EXIT_FAILURE;
}
if (!(tPPC[0] = malloc(1 + strlen(A)))) {
printf("failed allocating space for copy of A\n");
free(tPPC);
return EXIT_FAILURE;
}
if (!(tPPC[1] = malloc(1 + strlen(B)))) {
printf("failed allocating space for copy of B\n");
free(tPPC[0]);
free(tPPC);
return EXIT_FAILURE;
}
strcpy(tPPC[0], A);
strcpy(tPPC[1], B);
printf("%s\n", tPPC[0]);
printf("%s\n", tPPC[1]);
free(tPPC[0]);
free(tPPC[1]);
free(tPPC);
return 0;
}

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 );
}

--
Martin Ambuhl
Nov 14 '05 #2

"sieg1974" <si******@yahoo.com> wrote in message
news:89**************************@posting.google.c om...
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?

testPointerPointerChar is a pointer to a pointer to a char (or a pointer to
the first element of and array of pointers to chars (once the memory has
been assigned for the array).
2. Is testPointerPointerChar = malloc(...) allocating memory for this
matriz of pointers or for the block of chars?
In your code you are allocating a big block of memory and pointing
testPointerPointerChar to it.

if ( ( testPointerPointerChar = malloc( 50 * 256 * sizeof( char ) ) ) ==
NULL )

But since testPointerPointerChar is a pointer to a pointer to a char this
doesn't quite look correct at the moment (although you could quite feasibly
fill this block of memory with pointers to chars later).

3. With malloc( 50 * 256 * sizeof( char ) ) ) am I allocating memory
space for 50 string of 256 characters. Right?
You have just allocated a contiguous block of 50*256 bytes.. you haven't yet
defined how you are going to use this memory (You could put 50 null
terminated strings into it each spaced 256 bytes apart to simulate an array
of type char strings[50][256], or you could fill it with 320 pointers to
chars to simulate an array of type char * pStrings[320])

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.
*( testPointerPointerChar + 0 ) = A;
*( testPointerPointerChar + 1 ) = B;

Now you are defining how you are using the block of memory you have
allocated. Here you are using your block of memory as though it were an
array of pointers to chars (equivalent to char * testPointerPointer[320]).

So this is like saying

testPointerPointer[0]=A;
testPointerPointer[1]=B;

So the first 4 bytes of the memory block hold a pointer to A and the second
4 bytes of the block hold a pointer to B .

But when you do the following:

strcpy( *( testPointerPointerChar + 0 ), A );
strcpy( *( testPointerPointerChar + 1 ), B );

The first strcpy() is trying to access the first entry in your array of
pointers (char * testPointerPointer[320]). i.e testPointerPointerChar[0]
but you haven't made this point anywhere yet so it gives a segmentation
violation.

You would need to point it somewhere first: E.g.

testPointerPointerChar[0]=malloc(...);
strcpy( *( testPointerPointerChar + 0 ), A ); // note
testPointerPointerChar[0] and *(testPointerPointerChar+0) are equivalent
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?


If you just want 50 pointers to chars you would do:

char * strings[50];

Then allocate memory for each string array:

strings[0]=malloc(....);
strings[1]=malloc(....);

Or you could have

char ** strings;

then

strings=malloc(50*sizeof(char *));
strings[0]=malloc(....);
strings[1]=malloc(....);

hope this helps
Sean


Nov 14 '05 #3
On 18 Jan 2004 15:05:27 -0800, si******@yahoo.com (sieg1974) wrote:
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 )
This does not allocate the correct amount of memory. Your variable is
a pointer to pointer. Therefore, each object it points to is a
pointer to char, not a char. You need to replace sizeof(char) with
either sizeof(char*) or, preferably, sizeof *testPointerPointerChar.
Since you only use the first two pointers, this will not cause
problems in THIS code.
return( -1 );

/*
*( testPointerPointerChar + 0 ) = A;
Not incorrect but most prefer testPointerPointerChar[0] = ...
*( testPointerPointerChar + 1 ) = B;
*/

strcpy( *( testPointerPointerChar + 0 ), A );
This invokes undefined behavior. *(testPointerPointerChar+0) points
to a string literal. strcpy will attempt to replace the data in this
literal. Any attempt to modify a string literal is illegal.
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?
Close. testPointerPointerChar points to the first of a sequence of
pointers which can be treated as an array or matrix. For
testPointerPointerChar to actually have the type pointer to array of
pointers, it would need to be declared as
char *(*testPointerPointerChar)[N]

2. Is testPointerPointerChar = malloc(...) allocating memory for this
matriz of pointers or for the block of chars?
For the matrix of pointers. testPointerPointerChar is a char**. The
Thing it points to is always of the same type as
*testPointerPointerChar. *(char**) is of type char*.

3. With malloc( 50 * 256 * sizeof( char ) ) ) am I allocating memory
space for 50 string of 256 characters. Right?
Not at all. Since sizeof(char) is guaranteed to be 1, you are
allocating 12,800 bytes which is capable of holding
12800/sizeof(char*) pointers. After the return from malloc, none of
these pointers is initialized so they don't point anywhere.

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.
A segmentation fault is one of the better manifestations of undefined
behavior. Thank your compiler writer.

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
Why sizeof(int)? You are not allocating space for int. You are
allocating space for char*. Try 50 * sizeof(char*).
the memory used by a pointer the same as an integer?


That is an implementation detail. On many systems, sizeof(int) and
sizeof(char*) are both 4. BUT they are completely different types.
Be aware that if T1 and T2 are different types, there is no
requirement for sizeof(T1*) to be the same as sizeof(T2*) except in a
few special cases, such as void* and char*.
<<Remove the del for email>>
Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Yang Song | last post by:
HI, I am a little confused about char * and char. How would I be able to return a char* created in a function? Is new() the only way? How would I be able to return a point and a value at the same...
5
by: Roy Hills | last post by:
When I'm reading from or writing to a network socket, I want to use a struct to represent the structured data, but must use an unsigned char buffer for the call to sendto() or recvfrom(). I have...
5
by: jab3 | last post by:
(again :)) Hello everyone. I'll ask this even at risk of being accused of not researching adequately. My question (before longer reasoning) is: How does declaring (or defining, whatever) a...
3
by: s.subbarayan | last post by:
Dear all, I encountered the following piece of program: #include <stdio.h> void strprint(char *str); void main() { char *spr="hello"; strprint(spr); }
8
by: novice | last post by:
Hi geeks, Can any body explain me how to analyse int the pollowing code This is the question I was asked in the interview... char *s ={ "hello", "basic", "world", "program"}; char...
9
by: Bill | last post by:
Hi experts, I'm trying to return a char from a function. How would this be setup and what would the declaration look like? Thanks, Bill
20
by: liujiaping | last post by:
I'm confused about the program below: int main(int argc, char* argv) { char str1 = "abc"; char str2 = "abc"; const char str3 = "abc"; const char str4 = "abc"; const char* str5 = "abc";
11
by: john | last post by:
Hi, at first the code doesn't seem to work. Any ideas?: #include <iostream> #include <cstdlib> int main() { using namespace std;
16
by: s0suk3 | last post by:
This code #include <stdio.h> int main(void) { int hello = {'h', 'e', 'l', 'l', 'o'}; char *p = (void *) hello; for (size_t i = 0; i < sizeof(hello); ++i) {
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.