473,657 Members | 2,763 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

what difference between pointer and struct variable


dear,

I debug the program recently as follows.

#include <sys/stat.h>

int main(int argc, char *argv[])
{
struct stat buf;

stat(argv[1], &buf);

...
return 0;
}

int main(int argc, char *argv[])
{
struct stat *buf;

stat(argv[1], buf);

...

return 0;

}

what is the difference with them?
why I have to modify 2nd program like this:
{
struct stat temp, *buf = &temp;

stat(argv[1], buf);

...

}
many thanks

Nov 14 '05 #1
11 3403
"J Wang" <cs****@bath.ac .uk> wrote in message
news:Pi******** *************** *************** @amos.bath.ac.u k...

dear,

I debug the program recently as follows.

#include <sys/stat.h>

int main(int argc, char *argv[])
{
struct stat buf;

stat(argv[1], &buf);

...
return 0;
}

int main(int argc, char *argv[])
{
struct stat *buf;
This is a pointer to a type 'struct stat' object.
Since you did not initialize it or assign it a value,
this pointer's value is indeterminate (it doesn't
point anywhere.)

stat(argv[1], buf);
This statement passes the unknown (i.e. random) pointer
value to the function 'stat()'. If that function attempts
to dereference that pointer, the program's behavior becomes
undefined.

IOW you need a 'struct stat' object for the pointer to
point to, but did not provide one.
...

return 0;

}

what is the difference with them?
why I have to modify 2nd program like this:
{
struct stat temp, *buf = &temp;

stat(argv[1], buf);

...

}


Because you need an object for the pointer to point to.
A pointer definition does not automatically create an
object to point to. That's your job.

-Mike
Nov 14 '05 #2
J Wang wrote:
dear,

I debug the program recently as follows.

#include <sys/stat.h>

int main(int argc, char *argv[])
{
struct stat buf;

stat(argv[1], &buf);

...
return 0;
}

int main(int argc, char *argv[])
{
struct stat *buf;

stat(argv[1], buf);

...

return 0;

}

what is the difference with them?
why I have to modify 2nd program like this:
{
struct stat temp, *buf = &temp;

stat(argv[1], buf);

...

}
many thanks


The first example is fine because buf is a structure object. The
second doesn't work because there is no structure object. The third
works because temp is an object and buf points to it.
--
Joe Wright mailto:jo****** **@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #3
On Sun, 18 Jul 2004 14:24:03 GMT, "Mike Wahler"
<mk******@mkwah ler.net> wrote:
"J Wang" <cs****@bath.ac .uk> wrote in message
news:Pi******* *************** *************** *@amos.bath.ac. uk...

dear,

I debug the program recently as follows.

#include <sys/stat.h>

int main(int argc, char *argv[])
{
struct stat buf;

stat(argv[1], &buf);

...
return 0;
}

int main(int argc, char *argv[])
{
struct stat *buf;
This is a pointer to a type 'struct stat' object.
Since you did not initialize it or assign it a value,
this pointer's value is indeterminate (it doesn't
point anywhere.)

stat(argv[1], buf);


This statement passes the unknown (i.e. random) pointer
value to the function 'stat()'. If that function attempts
to dereference that pointer, the program's behavior becomes
undefined.


Actually, the program's behavior becomes undefined when it evaluates
the second argument in preparation for calling stat regardless of what
stat does with it.

IOW you need a 'struct stat' object for the pointer to
point to, but did not provide one.
...

return 0;

}

what is the difference with them?
why I have to modify 2nd program like this:
{
struct stat temp, *buf = &temp;

stat(argv[1], buf);

...

}


Because you need an object for the pointer to point to.
A pointer definition does not automatically create an
object to point to. That's your job.

-Mike


<<Remove the del for email>>
Nov 14 '05 #4
>>
I debug the program recently as follows.

#include <sys/stat.h>

int main(int argc, char *argv[])
{
struct stat buf;

stat(argv[1], &buf);

...
return 0;
}

int main(int argc, char *argv[])
{
struct stat *buf;


This is a pointer to a type 'struct stat' object.
Since you did not initialize it or assign it a value,
this pointer's value is indeterminate (it doesn't
point anywhere.)

stat(argv[1], buf);


This statement passes the unknown (i.e. random) pointer
value to the function 'stat()'. If that function attempts
to dereference that pointer, the program's behavior becomes
undefined.


This program invokes undefined behavior before the function stat()
is called, because an uninitialized pointer is used. The result
is undefined behavior whether or not stat() dereferences or even
uses its second argument.

Gordon L. Burditt
Nov 14 '05 #5
Like you said, the first example uses a variable, and the second one a
pointer, thus their behave is different. In the first example you
created a variable of the type 'struct stat' you passed it's address
using the '&' operator to the function stat. When you declared the
variable buf, you "reserved" a space in memory to store some data. So
when you pass the address of this space the function acts in it, doing
the job it's intended to.

In the second example, you declared a variable of the type 'pointer to
struct stat'. When you declare it, it's value can't be determined, so
it's what we call a 'junk pointer' and it's pointing to some place we
don't know. What you're doing is saying to the function stat to read
in this undetermined place, but since you don't know where it's
pointing, it may (and probably will) be in a place where you can't
access. So, in order to make your second example work, you should make
your pointer point to some place where you know you have access. To do
it you could use dynamic memory allocation...

int main(int argc, char *argv[])
{
struct stat *buf;

buf = malloc(sizeof(s truct stat));

stat(argv[1], buf);

...

free(buf);

return 0;

}

or you could do like in your third example.

J Wang <cs****@bath.ac .uk> wrote in message news:<Pi******* *************** *************** *@amos.bath.ac. uk>...
dear, I debug the program recently as follows. #include <sys/stat.h> int main(int argc, char *argv[]) { struct stat buf; stat(argv[1], &buf); ... return 0; } int main(int argc, char *argv[]) { struct stat *buf; stat(argv[1], buf); ... return 0; } what is the difference with them? why I have to modify 2nd program like this: { struct stat temp, *buf = &temp; stat(argv[1], buf); ... } many thanks

Nov 14 '05 #6
Gustavo Cipriano Mota Sousa <gu*********@in f.ufg.br> spoke thus:
struct stat *buf;
buf = malloc(sizeof(s truct stat));


1) OP should check whether malloc() succeeded, of course.
2) The preferred idiom is

buf=malloc( sizeof(*buf) );

because it will not break if the declaration of buf is altered.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #7

On Mon, 19 Jul 2004, Christopher Benson-Manica wrote:

Gustavo Cipriano Mota Sousa <gu*********@in f.ufg.br> spoke thus:
struct stat *buf;
buf = malloc(sizeof(s truct stat));


1) OP should check whether malloc() succeeded, of course.
2) The preferred idiom is

buf=malloc( sizeof(*buf) );

because it will not break if the declaration of buf is altered.


Except that the redundant parentheses might as well be lost, too.
Simplicity is clarity. (Note also how my religion w.r.t. whitespace
is basically the exact opposite of Chris's;)

buf = malloc(sizeof *buf);

-Arthur

Nov 14 '05 #8
Arthur J. O'Dwyer <aj*@nospam.and rew.cmu.edu> spoke thus:
Except that the redundant parentheses might as well be lost, too.
Simplicity is clarity. (Note also how my religion w.r.t. whitespace
is basically the exact opposite of Chris's;) buf = malloc(sizeof *buf);


It's my employer's religion, actually, but yes :)

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #9
"Christophe r Benson-Manica" <at***@nospam.c yberspace.org> wrote in message
news:cd******** **@chessie.cirr .com...
Gustavo Cipriano Mota Sousa <gu*********@in f.ufg.br> spoke thus:
struct stat *buf;
buf = malloc(sizeof(s truct stat));


1) OP should check whether malloc() succeeded, of course.
2) The preferred idiom is

buf=malloc( sizeof(*buf) );

because it will not break if the declaration of buf is altered.


Except if buf is altered to an incorrect declaration. Not casting malloc does not save the
programmer in this regard.

--
Peter
Nov 14 '05 #10

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

Similar topics

140
7815
by: Oliver Brausch | last post by:
Hello, have you ever heard about this MS-visual c compiler bug? look at the small prog: static int x=0; int bit32() { return ++x; }
100
5236
by: E. Robert Tisdale | last post by:
What is an object? Where did this term come from? Does it have any relation to the objects in "object oriented programming"?
4
2108
by: Ole | last post by:
hello, Little problem: struct operatable { char * operatable_id; int ( * delegate ) ( ... ); somedatatype data; };
5
2528
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 going to hell just by using dlopen()/LoadLibrary() respectively, I'm still trying to get it as clean as possible. I have a number of different quantums of data and a number of plugins. Since any plugin can (and possibly will) touch any quantum...
4
4112
by: bingfeng | last post by:
I have some codes generated by perl, in which initialize some huge struct,such as PARA TOS_network_spantree_set_0_para_0 = { "vlan", emNUM, NULL, "", "configuration on a designated vlan", PRO_REQUIRED }; const char* TOS_network_spantree_set_0_para_1_emvalue = { "disable", "enable", NULL }; PARA TOS_network_spantree_set_0_para_1 = { "", emENUM, TOS_network_spantree_set_0_para_1_emvalue, "", "enable or disable STP", PRO_REQUIRED };
5
1932
by: RocTheEngy | last post by:
Greetings c.l.c... I am trying to understand some structure definitions in working code (e.g. compiles, runs & produces expected results) that I have. I think I've trimmed the code to what is relevant to my question... Which is: What is the (*vector) in the "struct function" declaration. What is that line doing? My best guess is that it is a function definition that returns a struct
5
8726
by: Chris | last post by:
Hi, I don't get the difference between a struct and a class ! ok, I know that a struct is a value type, the other a reference type, I understand the technical differences between both, but conceptually speaking : when do I define something as 'struct' and when as 'class' ? for example : if I want to represent a 'Time' thing, containing : - data members : hours, mins, secs
5
3603
by: alcool | last post by:
hi, I have 2 date/time values i.e. the system date/time and (h:m dd:mm:yyyy). I would like know to find a routine that calculate this difference. Maybe using the struct time_t and difftime. how I can do? Please help me.
11
3665
by: Neo | last post by:
If i have a : typedef struct str32{ uint32_t word1; uint32_t word2; } word_array; word_array *my_array; what would be the data type of this: myarray->word1
0
8394
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
8732
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
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
7327
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
5632
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
4152
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
2726
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
1955
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1615
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.