473,772 Members | 2,391 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

char ** questions

Hi,

I have made this simple program to understand char ** pointers, but I
still having many questions.

int main()
{
char ** testPointerPoin terChar = 0;

char * A = "string01";
char * B = "string02";
if ( ( testPointerPoin terChar = malloc( 50 * 256 * sizeof( char ) ) )
== NULL )
return( -1 );

/*
*( testPointerPoin terChar + 0 ) = A;
*( testPointerPoin terChar + 1 ) = B;
*/

strcpy( *( testPointerPoin terChar + 0 ), A );
strcpy( *( testPointerPoin terChar + 1 ), B );

printf( "%s\n", *( testPointerPoin terChar + 0 ) );
printf( "%s\n", *( testPointerPoin terChar + 1 ) );

return( 0 );
}

1. testPointerPoin terChar pointer points to a matriz of pointers, and
each one of them points to a char. Right?

2. Is testPointerPoin terChar = 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. *( testPointerPoin terChar + 1 ) = B seems to work fine, making the
second pointer to point to B, but why strcpy( *(
testPointerPoin terChar + 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 2211
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 ** testPointerPoin terChar = 0;

char * A = "string01";
char * B = "string02";
if ( ( testPointerPoin terChar = malloc( 50 * 256 * sizeof( char ) ) )
== NULL )
return( -1 );

/*
*( testPointerPoin terChar + 0 ) = A;
*( testPointerPoin terChar + 1 ) = B;
*/

strcpy( *( testPointerPoin terChar + 0 ), A );
strcpy( *( testPointerPoin terChar + 1 ), B );

printf( "%s\n", *( testPointerPoin terChar + 0 ) );
printf( "%s\n", *( testPointerPoin terChar + 1 ) );

return( 0 );
}

--
Martin Ambuhl
Nov 14 '05 #2

"sieg1974" <si******@yahoo .com> wrote in message
news:89******** *************** ***@posting.goo gle.com...
Hi,

I have made this simple program to understand char ** pointers, but I
still having many questions.

int main()
{
char ** testPointerPoin terChar = 0;

char * A = "string01";
char * B = "string02";
if ( ( testPointerPoin terChar = malloc( 50 * 256 * sizeof( char ) ) )
== NULL )
return( -1 );

/*
*( testPointerPoin terChar + 0 ) = A;
*( testPointerPoin terChar + 1 ) = B;
*/

strcpy( *( testPointerPoin terChar + 0 ), A );
strcpy( *( testPointerPoin terChar + 1 ), B );

printf( "%s\n", *( testPointerPoin terChar + 0 ) );
printf( "%s\n", *( testPointerPoin terChar + 1 ) );

return( 0 );
}

1. testPointerPoin terChar pointer points to a matriz of pointers, and
each one of them points to a char. Right?

testPointerPoin terChar 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 testPointerPoin terChar = 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
testPointerPoin terChar to it.

if ( ( testPointerPoin terChar = malloc( 50 * 256 * sizeof( char ) ) ) ==
NULL )

But since testPointerPoin terChar 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. *( testPointerPoin terChar + 1 ) = B seems to work fine, making the
second pointer to point to B, but why strcpy( *(
testPointerPoin terChar + 1 ), B ) doesn't work? It generates a
segmentation fault when this program is executed.
*( testPointerPoin terChar + 0 ) = A;
*( testPointerPoin terChar + 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 * testPointerPoin ter[320]).

So this is like saying

testPointerPoin ter[0]=A;
testPointerPoin ter[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( *( testPointerPoin terChar + 0 ), A );
strcpy( *( testPointerPoin terChar + 1 ), B );

The first strcpy() is trying to access the first entry in your array of
pointers (char * testPointerPoin ter[320]). i.e testPointerPoin terChar[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.

testPointerPoin terChar[0]=malloc(...);
strcpy( *( testPointerPoin terChar + 0 ), A ); // note
testPointerPoin terChar[0] and *(testPointerPo interChar+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 ** testPointerPoin terChar = 0;

char * A = "string01";
char * B = "string02";
if ( ( testPointerPoin terChar = 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 *testPointerPoi nterChar.
Since you only use the first two pointers, this will not cause
problems in THIS code.
return( -1 );

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

strcpy( *( testPointerPoin terChar + 0 ), A );
This invokes undefined behavior. *(testPointerPo interChar+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( *( testPointerPoin terChar + 1 ), B );

printf( "%s\n", *( testPointerPoin terChar + 0 ) );
printf( "%s\n", *( testPointerPoin terChar + 1 ) );

return( 0 );
}

1. testPointerPoin terChar pointer points to a matriz of pointers, and
each one of them points to a char. Right?
Close. testPointerPoin terChar points to the first of a sequence of
pointers which can be treated as an array or matrix. For
testPointerPoin terChar to actually have the type pointer to array of
pointers, it would need to be declared as
char *(*testPointerP ointerChar)[N]

2. Is testPointerPoin terChar = malloc(...) allocating memory for this
matriz of pointers or for the block of chars?
For the matrix of pointers. testPointerPoin terChar is a char**. The
Thing it points to is always of the same type as
*testPointerPoi nterChar. *(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. *( testPointerPoin terChar + 1 ) = B seems to work fine, making the
second pointer to point to B, but why strcpy( *(
testPointerPoi nterChar + 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
3541
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 time? I thought of a possible solution. However, I am not quite sure if I understood char vs. char*. Any insight is greatly appreciated. Here is the question: ---------------------- ....
5
17653
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 two questions: 1. Is it generally safe to "overlay" the structure on the buffer, e.g.: unsigned char buffer;
5
3980
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 variable **var make it an array of pointers? I realize that 'char **var' is a pointer to a pointer of type char (I hope). And I realize that with var, var is actually a memory address (or at
3
1990
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
2098
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 **sPtr = { s+3, s+2, s+1, s };
9
8248
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
3566
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
2956
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
6798
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
9619
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10261
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8934
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6713
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.