Consider the following declaration,
#include <stdio.h>
#include <stdlib.h>
typedef struct foo {
char name[30];
int age;
}Foo;
typedef struct bar {
char * name;
int age;
}Bar;
typedef struct baz {
char name[30];
int *age;
}Baz;
int main()
{
Foo f1,f2;
Bar b1,b2;
Baz c1,c2;
/* Populating structure */
strcpy(f1.name, "JACK");
f1.age=10;
/* CASE 1 - Works fine */
f2=f1;
printf("\n F2 Members, Name %s , Age %d ", f2.name, f2.age);
printf("\n F1 Pointer %p , F2 Pointer %p ", f1, f2);
/* CASE 2 - Hmmmm.. I don't have an explanation for this */
b1.name = malloc(sizeof(c har)*10);
strcpy(b1.name, "LACK");
b1.age=20;
b2=b1; /*Assignment??*/
printf("\n B2 Members, Name %s , Age %d ", b2.name, b2.age);
printf("\n B1 Pointer %p , B2 Pointer %p ", b1, b2);
/* This will print the same address out. */
printf("\n B1 Name Pointer %p , B2 Name Pointer %p ", b1.name, \
b2.name);
/* CASE 3 - As expected, does not work */
c1.age = malloc(sizeof(i nt));
strcpy(c1.name, "MACK");
*(c1.age)=30;
c2=c1; /*Assignment??*/
printf("\n C2 Members, Name %s , Age %d ", c2.name, c2.age);
printf("\n C1 Pointer %p , C2 Pointer %p ", c1, c2);
/* But this will print the same address out, how come? */
printf("\n C1 Name Pointer %p , C2 Name Pointer %p ", c1.age ,\
c2.age );
exit(EXIT_SUCCE SS);
}
Let me begin with apologies for posting so much of code.
Clarification 1 : Is copying structures by simply assigning them, valid
(legal) portable, pedantic ?
<My Opinion> : No. If someone argues, please reason.
Clarification 2 : Why is case 2 working ?
Clarification 3 : In case 3, you will notice the address copied exactly.
Does copying address mean pointing to the same
location? 4 4691
Trying_Harder wrote: Clarification 1 : Is copying structures by simply assigning them, valid (legal) portable, pedantic ? <My Opinion> : No. If someone argues, please reason.
It's legal and portable (see standard) Clarification 2 : Why is case 2 working ?
Why would you not expect it to work?
Clarification 3 : In case 3, you will notice the address copied exactly. Does copying address mean pointing to the same location?
It does, but you'd do better with:
printf("\n C2 Members, Name %s , Age %d ", c2.name, *c2.age);
since c2.age is a pointer (as you remembered in your next statement.
--
Morris Dovey
West Des Moines, Iowa USA
C links at http://www.iedu.com/c
Trying_Harder wrote: Consider the following declaration,
#include <stdio.h> #include <stdlib.h>
#include <string.h> /* for function strcpy */ typedef struct foo { char name[30]; int age; }Foo;
typedef struct bar { char * name; int age; }Bar;
typedef struct baz { char name[30]; int *age; }Baz;
int main() { Foo f1,f2; Bar b1,b2; Baz c1,c2;
/* Populating structure */ strcpy(f1.name, "JACK"); f1.age=10;
/* CASE 1 - Works fine */ f2=f1; printf("\n F2 Members, Name %s , Age %d ", f2.name, f2.age); printf("\n F1 Pointer %p , F2 Pointer %p ", f1, f2);
%p specifies a void* argument. f1 and f2 need to be pointers that
should be cast to void*.
printf("\n F1 Pointer %p, F2 Pointer %p ", (void*)&f1, (void*)&f2); /* CASE 2 - Hmmmm.. I don't have an explanation for this */ b1.name = malloc(sizeof(c har)*10); strcpy(b1.name, "LACK"); b1.age=20;
b2=b1; /*Assignment??*/ printf("\n B2 Members, Name %s , Age %d ", b2.name, b2.age); printf("\n B1 Pointer %p , B2 Pointer %p ", b1, b2); /* This will print the same address out. */ printf("\n B1 Name Pointer %p , B2 Name Pointer %p ", b1.name, \ b2.name); /* CASE 3 - As expected, does not work */ c1.age = malloc(sizeof(i nt)); strcpy(c1.name, "MACK"); *(c1.age)=30;
c2=c1; /*Assignment??*/ printf("\n C2 Members, Name %s , Age %d ", c2.name, c2.age);
*c2.age
printf("\n C1 Pointer %p , C2 Pointer %p ", c1, c2);
/* But this will print the same address out, how come? */ printf("\n C1 Name Pointer %p , C2 Name Pointer %p ", c1.age ,\ c2.age );
Perhaps you are being confused by the above printf statement. You
are printing "Name Pointer" but your arguments refer to the age members.
Shouldn't these be:
printf("C1 Name Pointer %p , C2 Name Pointer %p\n",
(void*) c1.name ,(void*)c2.name );
printf("C1 Age Pointer %p , C2 Age Pointer %p\n", (void*)c1.age ,
(void*)c2.age ); exit(EXIT_SUCCE SS); }
Clarification 3 : In case 3, you will notice the address copied exactly. Does copying address mean pointing to the same location?
Try the corrected code and see if you still need clarification.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct foo {
char name[30];
int age;
}Foo;
typedef struct bar {
char * name;
int age;
}Bar;
typedef struct baz {
char name[30];
int *age;
}Baz;
int main(void)
{
Foo f1,f2;
Bar b1,b2;
Baz c1,c2;
strcpy(f1.name, "JACK");
f1.age=10;
f2=f1;
printf("F2 Members, Name %s , Age %d\n", f2.name, f2.age);
printf("F1 Pointer %p , F2 Pointer %p\n", (void*)&f1, (void*)&f2);
b1.name = malloc(sizeof(c har)*10);
strcpy(b1.name, "LACK");
b1.age=20;
b2=b1; /*Assignment??*/
printf("B2 Members, Name %s , Age %d\n", b2.name, b2.age);
printf("B1 Pointer %p , B2 Pointer %p\n", (void*)&b1, (void*)&b2);
/* This will print the same address out. */
printf("B1 Name Pointer %p , B2 Name Pointer %p\n",
(void*)b1.name, (void*)b2.name) ;
c1.age = malloc(sizeof(i nt));
strcpy(c1.name, "MACK");
*(c1.age)=30;
c2=c1; /*Assignment??*/
printf("C2 Members, Name %s , Age %d\n", c2.name, *c2.age);
printf("C1 Pointer %p , C2 Pointer %p\n", (void*)&c1, (void*)&c2);
printf("C1 Name Pointer %p , C2 Name Pointer %p\n",
(void*) c1.name ,(void*)c2.name );
printf("C1 Age Pointer %p , C2 Age Pointer %p\n", (void*)c1.age ,
(void*)c2.age );
return 0;
}
--
Al Bowers
Tampa, Fl USA
mailto: xa*@abowers.com base.com (remove the x) http://www.geocities.com/abowers822/
On Mon, 15 Sep 2003 22:30:44 -0700, Trying_Harder wrote: #include <stdio.h> #include <stdlib.h>
#include <string.h>
typedef struct baz { char name[30]; int *age; }Baz;
int main() { Foo f1,f2; Bar b1,b2; Baz c1,c2;
/* Populating structure */ strcpy(f1.name, "JACK"); f1.age=10;
/* CASE 1 - Works fine */ printf("\n F1 Pointer %p , F2 Pointer %p ", f1, f2);
This may work but is not right. Try:
printf("\n F1 Pointer %p , F2 Pointer %p ", &f1, &f2); printf("\n B1 Pointer %p , B2 Pointer %p ", b1, b2);
And again....
printf("\n B1 Pointer %p , B2 Pointer %p ", &b1, &b2); printf("\n C2 Members, Name %s , Age %d ", c2.name, c2.age);
You'll need to dereference c2.age to get its value:
printf("\n C2 Members, Name %s , Age %d ", c2.name, *c2.age);
printf("\n C1 Pointer %p , C2 Pointer %p ", c1, c2);
I hate to be repetitive.. but...
printf("\n C1 Pointer %p , C2 Pointer %p ", &c1, &c2); /* But this will print the same address out, how come? */ printf("\n C1 Name Pointer %p , C2 Name Pointer %p ", c1.age ,\ c2.age );
free(b1.name);
free(c1.age);
exit(EXIT_SUCCE SS); }
Let me begin with apologies for posting so much of code.
Clarification 1 : Is copying structures by simply assigning them, valid (legal) portable, pedantic ?
legal and portable. Clarification 2 : Why is case 2 working ?
Other than the fact you are not freeing the memory you've allocated I
don't see any reason as to why it shouldn't work.
Clarification 3 : In case 3, you will notice the address copied exactly. Does copying address mean pointing to the same location?
Yep.
- Jake
Jake Roersma <ja***@copiosus .net> wrote: On Mon, 15 Sep 2003 22:30:44 -0700, Trying_Harder wrote:
<SNIP> printf("\n F1 Pointer %p , F2 Pointer %p ", f1, f2);
This may work but is not right. Try:
printf("\n F1 Pointer %p , F2 Pointer %p ", &f1, &f2);
The printf %p conversion specification requires a void pointer argument;
so make it:
printf("\n F1 Pointer %p , F2 Pointer %p ", (void *)&f1, (void *)&f2); printf("\n B1 Pointer %p , B2 Pointer %p ", b1, b2);
And again....
printf("\n B1 Pointer %p , B2 Pointer %p ", &b1, &b2);
.... and again ...
printf("\n B1 Pointer %p , B2 Pointer %p ", (void *)&b1, (void *)&b2); printf("\n C2 Members, Name %s , Age %d ", c2.name, c2.age);
You'll need to dereference c2.age to get its value:
printf("\n C2 Members, Name %s , Age %d ", c2.name, *c2.age);
printf("\n C1 Pointer %p , C2 Pointer %p ", c1, c2);
I hate to be repetitive.. but...
printf("\n C1 Pointer %p , C2 Pointer %p ", &c1, &c2);
.... me too, but ... ;)
printf("\n C1 Pointer %p , C2 Pointer %p ", (void *)&c1, (void *)&c2);
<SNIP>
Note that the only useful application for printing out pointer values I
know of is for debugging purposes.
Regards
Irrwahn
--
What does this red button do? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: simon |
last post by:
From my previous post...
If I have a structure,
struct sFileData
{
char*sSomeString1;
char*sSomeString2;
int iSomeNum1;
int iSomeNum2;
|
by: Mannequin* |
last post by:
Hi all,
I'm working on a quick program to bring the Bible into memory from a
text file. Anyway, I have three questions to ask.
First, is my implementation of malloc () correct in the program to
follow?
Second, have I correctly passed the structure's pointer to the
functions in this program?
|
by: Excluded_Middle |
last post by:
Suppose I have a struct
typdef struct foo
{
int age;
char *name;
}foo;
now I made a list of foo using
|
by: Duncan Mole |
last post by:
Hi,
This is probably an easy one but it iy first bit of p/invoke. I am trying to
use the following C struct in a call:
typedef struct
{
BYTE SRB_Cmd;
BYTE SRB_Status,
BYTE SRB_HaId;
|
by: pra_ramli |
last post by:
Hi all,
While coding, I got struck up in memory allocation for structure to
structure..
For instance..I wrote the following pgm..
#include <iostream.h>
#include <stdlib.h>
typedef struct A
{
| |
by: hobbes992 |
last post by:
Howdy folks,
I've been working on a c project, compiling using gcc, and I've reached a problem. The assignment requires creation of a two-level directory file system. No files have to be added or deleted, however it must be initialized by a function during run-time to contain so many users which each contain so many directories of which each contain so many files. I've completed the program and have it running flawlessly without implementing...
|
by: dtschoepe |
last post by:
Hi,
I have a homework project I am working on, so be forwarned, I'm new to
C programming. But anyway, having some trouble with a memory
allocation issue related to a char * that is a variable inside of a
structure. I keep getting segmentation fault errors and I am having
trouble understanding why. Here's the parts of the code in question...
This is part of the .h file where the struct us defined...
|
by: uday |
last post by:
Hi All,
I need one clarification regarding memory allocation for
structure. The details are as given below :
let us consider one structure
struct {
uit32 len0;
uint8 *pointer0;
uit32 len1;
|
by: rahul |
last post by:
How is the memory allocated for structures? I need to optimize the
memory usage and bit fields are not doing the trick.
Any details about the memory allocation for the structures would be a
great help.
PS - I already have asked this in gcc group. Please refrain from
directing me towards other groups.
|
by: Andrew Smallshaw |
last post by:
I'm working on a data structure that began life as a skip list
derivative, but has evolved to the point that it now only has a
passing resemblance to them. Each node of this structure has a
few constant size variables (ints and the like), an array holding
the actual data (chars in this case) and a random number of pointers
to other nodes in the structure.
At the moment my code uses a fixed size array for the contents
(which may or may...
|
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...
| |
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,...
|
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...
|
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,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
| |
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();...
|
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...
| |