473,385 Members | 1,942 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,385 software developers and data experts.

Pointer to a Pointer to a struct

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

int
main(void)
{
typedef struct
{
char **test;
}Testing;

Testing **p;
p = malloc(sizeof(Testing));
p[0]->test[0] = malloc(sizeof(Testing));
strncpy(p[0]->test[0],"Hello World",20);
printf("%s\n",p[0]->test[0]);

return EXIT_SUCCESS;
}

Why does the following crash ?
Not sure what i'm missing please advise thanks.
--
Unix Systems Engineer
The City of New York
Dept. of Information Technology
http://www.nyc.gov/doitt
rbrown[(@)]doitt.nyc.gov
http://www.rodrickbrown.com

Nov 14 '05 #1
6 6703
Rodrick Brown <rbrown[@]doitt.nyc.gov> scribbled the following:
#include <stdio.h>
#include <string.h>
#include <stdlib.h> int
main(void)
{
typedef struct
{
char **test;
}Testing; Testing **p;
p = malloc(sizeof(Testing));
p[0]->test[0] = malloc(sizeof(Testing));
You haven't allocated any memory for p[0], let alone for p[0]->test.
strncpy(p[0]->test[0],"Hello World",20);
And anyway, sizeof(Testing) bytes might very well be too little to
store "Hello World".
printf("%s\n",p[0]->test[0]); return EXIT_SUCCESS;
} Why does the following crash ?
Not sure what i'm missing please advise thanks.


I think the fault is that you don't properly understand double
pointers.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"The truth is out there, man! Way out there!"
- Professor Ashfield
Nov 14 '05 #2

Joona I Palaste wrote:
Rodrick Brown <rbrown[@]doitt.nyc.gov> scribbled the following:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>


int
main(void)
{
typedef struct
{
char **test;
}Testing;


Testing **p;
p = malloc(sizeof(Testing));
p[0]->test[0] = malloc(sizeof(Testing));

You haven't allocated any memory for p[0], let alone for p[0]->test.

strncpy(p[0]->test[0],"Hello World",20);

And anyway, sizeof(Testing) bytes might very well be too little to
store "Hello World".

printf("%s\n",p[0]->test[0]);


return EXIT_SUCCESS;
}


Why does the following crash ?
Not sure what i'm missing please advise thanks.

I think the fault is that you don't properly understand double
pointers.


Joona I Palaste means "pointers to pointers" as in ** and not
as in double *. Just to clarify.

Read section 6 of the C FAQ (on arrays and pointers):
http://www.eskimo.com/~scs/C-faq/s6.html

Cheers
Michael

Nov 14 '05 #3
Michael Mair wrote:

Joona I Palaste wrote:

<snip>


I think the fault is that you don't properly understand double
pointers.

Joona I Palaste means "pointers to pointers" as in ** and not
as in double *. Just to clarify.

<snip>

To nitpick, I think Joona meant "double indirection". Not that it
really matters.
Mark F. Haigh
mf*****@sbcglobal.net
Nov 14 '05 #4
Joona I Palaste wrote:
Rodrick Brown <rbrown[@]doitt.nyc.gov> scribbled the following:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int
main(void)
{
typedef struct
{
char **test;
}Testing;


Testing **p;
p = malloc(sizeof(Testing));
p[0]->test[0] = malloc(sizeof(Testing));

You haven't allocated any memory for p[0], let alone for p[0]->test.


The "testing" structure contains only one member:
a pointer that points to a pointer to characters.
The size of a pointer is (assumed) 4 (32 bit system).

Then, your structure is just 4 bytes long.

Another thing would be:
typedef struct { char Test[256]; }Testing;

*THEN* you would be able to do your copy
I think the fault is that you don't properly understand double
pointers.


Exactly
Nov 14 '05 #5
Rodrick Brown <rbrown[@]doitt.nyc.gov> wrote in message news:<2004101503155216807%rbrown@doittnycgov>...
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int
main(void)
{
typedef struct
{
char **test;
}Testing;

Testing **p;
p = malloc(sizeof(Testing));
You have a mismatch here. You're allocating memory for an object of
type Testing, but assigning the resulting pointer to an object of type
pointer to pointer to Testing.

type of p == Testing **, value == return value of malloc()
type of p[0] == Testing *, value == indeterminate
p[0]->test[0] = malloc(sizeof(Testing));
You've missed a step in your allocation -- p points somewhere
meaningful after the first malloc() (even with the mismatch), but p[0]
does not, so you're attempting to dereference an invalid pointer.
This is why you are crashing.
strncpy(p[0]->test[0],"Hello World",20);
Same thing here. You've skipped an allocation step (although you've
already crashed by now anyway; besides, you likely haven't allocated
enough memory to hold the string if you're going by the size of the
struct).
printf("%s\n",p[0]->test[0]);

return EXIT_SUCCESS;
}

Why does the following crash ?
Not sure what i'm missing please advise thanks.


You're doing double indirection, so you have to do double allocation:

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

int main (void)
{
typedef struct {
char **test;
} Testing;

Testing **p;

/* type of p[0] == Testing * */
p = malloc(sizeof p[0]);
if (!p)
return 1;

/* type of *p[0] == Testing */
p[0] = malloc(sizeof *p[0]);
if (!p[0])
return 1;

/* type of p[0]->test[0] == char * */
p[0]->test = malloc(sizeof p[0]->test[0]);
if (!p[0]->test)
return 1;

/* type of *p[0]->test[0] == char */
p[0]->test[0] = malloc (sizeof *p[0]->test[0] * 20);
if (!p[0]->test[0])
return 1;

strcpy (p[0]->test[0], "Hello World");

printf ("%s\n", p[0]->test[0]);

free(p[0]->test[0]);
free(p[0]->test);
free(p[0]);
free(p);

return 0;
}
Nov 14 '05 #6
Rodrick Brown <rbrown[@]doitt.nyc.gov> wrote in message news:<2004101503155216807%rbrown@doittnycgov>...
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int
main(void)
{
typedef struct
{
char **test;
}Testing;

Testing **p;
p = malloc(sizeof(Testing));
p[0]->test[0] = malloc(sizeof(Testing));
strncpy(p[0]->test[0],"Hello World",20);
printf("%s\n",p[0]->test[0]);

return EXIT_SUCCESS;
}

Why does the following crash ?
Not sure what i'm missing please advise thanks.


strncpy(p[0]->test[0],"Hello World",20);

you have not allocated sufficent amount of memory to the test.

Therefore,when you'll copy the string "hello world" to test,

you will be overwriting the address of the main(void) function.

As a result,the program will crash as soon as return statement will
be executed.

Aditya
Nov 14 '05 #7

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

Similar topics

3
by: sathyashrayan | last post by:
The standard confirms that the following initialization of a struct struct node { --- --- } struct node var = {NULL};
10
by: Kieran Simkin | last post by:
Hi, I wonder if anyone can help me, I've been headscratching for a few hours over this. Basically, I've defined a struct called cache_object: struct cache_object { char hostname; char ipaddr;...
5
by: Danilo Kempf | last post by:
Folks, maybe one of you could be of help with this question: I've got a relatively portable application which I'm extending with a plugin interface. While portability (from a C perspective) is...
2
by: Immo Birnbaum | last post by:
Hi, I'm trying to solve a programming lab assignment for my college C programming course, but as they taught us two semesters of Java before teaching us any C, I'm having problems with all the...
4
by: JS | last post by:
I have a file called test.c. There I create a pointer to a pcb struct: struct pcb {   void *(*start_routine) (void *);   void *arg;   jmp_buf state;   int    stack; }; ...
10
by: junky_fellow | last post by:
K&R say that, It is guaranteed that 1) a pointer to an object may be converted to a pointer to an object whose type requires less or equally strict storage alignment and 2) back again without...
5
by: Johs32 | last post by:
I have a struct "my_struct" and a function that as argument takes a pointer to this struct: struct my_struct{ struct my_struct *new; }; void my_func(struct my_struct *new); I have read...
2
by: Chad | last post by:
The following question stems from the following thread on comp.lang.c: http://groups.google.com/group/comp.lang.c/browse_thread/thread/0ad03c96df57381a/5f20260b30952fe7?hl=en#5f20260b30952fe7 I...
12
by: gcary | last post by:
I am having trouble figuring out how to declare a pointer to an array of structures and initializing the pointer with a value. I've looked at older posts in this group, and tried a solution that...
2
by: Mike | last post by:
Hi, I am new to C and having problems with the following program. Basically I am trying to read some files, loading data structures into memory for latter searching. I am trying to use structres...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...

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.