473,809 Members | 2,591 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Where the code trouble is?

Regards:

Where the code trouble is?
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(){

char *name;
char *title;
printf( "Enter your name:");
scanf( "%s", name );
title = strcat(name,"th e Great");
printf( "Hello, %s\n", title );
return(0);
}

--------------------------------------------

thank you
may goodness be with you all

Nov 15 '05
16 1612
"Bart C" <bc@freeuk.co m> wrote:
"mikelinyoh o" <mi*********@gm ail.com> wrote in message
news:11******** *************@z 14g2000cwz.goog legroups.com...
char *name;
char *title;
printf( "Enter your name:");
scanf( "%s", name );
The compiler might give a warning that 'name' is not initialised.


Not necessarily.
I would have expected scanf() to give an error when 'name' was (as I
assumed) null.

It fact on my test compiler 'name' has some arbitrary value (the program
worked),
No, it didn't. It merely appeared to.
So ideally, there should be (1) a warning (2) 'name' is best set to null
when not initialised, to avoid undefined action, and


Writing through a null pointer is no more defined than writing through a
wild pointer.
title = strcat(name,"th e Great");


A space might help


A space would not make the code any more correct. The real problem on
this line is similar to the one above.

Richard
Nov 15 '05 #11

"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message
news:43******** ********@news.x s4all.nl...
....
So ideally, there should be (1) a warning (2) 'name' is best set to null
when not initialised, to avoid undefined action, and


Writing through a null pointer is no more defined than writing through a
wild pointer.


Maybe, but an error is more or less guaranteed with a null pointer and makes
it easy to check for. An arbitrary wild pointer is difficult to check for
and leads to code that works on test then fails unexpectedly.

Bart.
Nov 15 '05 #12
In article <43******@212.6 7.96.135>, Bart C <bc@freeuk.co m> wrote:
"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message
news:43******* *********@news. xs4all.nl...
Writing through a null pointer is no more defined than writing through a
wild pointer.

Maybe, but an error is more or less guaranteed with a null pointer and makes
it easy to check for.


I know of at least two architectures in which NULL is all-zero-bits
but in which writing at (or near that location, such as a[3] when
a is NULL) would NOT result in a trap, because the architectures happen
to have memory at that location.
--
"[...] it's all part of one's right to be publicly stupid." -- Dave Smey
Nov 15 '05 #13
Walter Roberson wrote:
In article <43******@212.6 7.96.135>, Bart C <bc@freeuk.co m> wrote:
"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message
news:43****** **********@news .xs4all.nl...

Writing through a null pointer is no more defined than writing through a
wild pointer.

Maybe, but an error is more or less guaranteed with a null pointer and
makes it easy to check for.


I know of at least two architectures in which NULL is all-zero-bits
but in which writing at (or near that location, such as a[3] when
a is NULL) would NOT result in a trap, because the architectures happen
to have memory at that location.


The vast majority of machines in use have NULL as a trap location. Richard's
development system likely does so as well. Although *NULL might well not
trap, it's in general more likely to, which can help with debugging.

This will not catch all such errors of course, but it doesn't hurt anything,
except for causing a tiny (probably imperceptible) performance hit, and may
help with debugging.

--
λz.λi.i(i((λ n.λm.λz.λi.n z(λq.mqi))((λ n.λz.λi.n(nzi )i)(λz.λi.i(( (λn.λz.λi.n
(nzi)i)(λz.λi .i(iz)))zi)))(( λn.λz.λi.n(n zi)i)(λz.λi.i (iz)))zi))
Nov 15 '05 #14
Bryan Donlan wrote:
Walter Roberson wrote:
In article <43******@212.6 7.96.135>, Bart C <bc@freeuk.co m> wrote:
"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message
news:43***** ***********@new s.xs4all.nl...

Writing through a null pointer is no more defined than writing through a
wild pointer.

Maybe, but an error is more or less guaranteed with a null pointer and
makes it easy to check for.


I know of at least two architectures in which NULL is all-zero-bits
but in which writing at (or near that location, such as a[3] when
a is NULL) would NOT result in a trap, because the architectures happen
to have memory at that location.


The vast majority of machines in use have NULL as a trap location. Richard's
development system likely does so as well. Although *NULL might well not
trap, it's in general more likely to, which can help with debugging.

This will not catch all such errors of course, but it doesn't hurt anything,
except for causing a tiny (probably imperceptible) performance hit, and may
help with debugging.


Agreed.

I would also say that even on systems where writing to *NULL does not
trap, it is still likely (but not guaranteed) to give reproduceable
results which can be debugged.

For similar reasons on embedded systems I have changed the startup code
so that all RAM was initialised to all bits 0. I did not check on the
system whether NULL was all bits 0 or whether float or double 0 where
all bits 0, but it did meen the software was more predictable and easier
to debug when accessing uninitialised data. On another system they
initialised all RAM to 0x55 for the similar reason (I used all bits 0
because static variables without an initialiser where *not* set to 0).

IMHO the first stage in trying to debug a problem is always to reproduce
it, since if you can't reliably reproduce it you will never know if you
have fixed what caused the original bug report or something else that
you think might have caused it but did not.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 15 '05 #15
Bryan Donlan <bd*****@gmail. com> wrote:
Walter Roberson wrote:
In article <43******@212.6 7.96.135>, Bart C <bc@freeuk.co m> wrote:
"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message
news:43****** **********@news .xs4all.nl...

Writing through a null pointer is no more defined than writing through a
wild pointer.

Maybe, but an error is more or less guaranteed with a null pointer and
makes it easy to check for.


I know of at least two architectures in which NULL is all-zero-bits
but in which writing at (or near that location, such as a[3] when
a is NULL) would NOT result in a trap, because the architectures happen
to have memory at that location.


The vast majority of machines in use have NULL as a trap location. Richard's
development system likely does so as well. Although *NULL might well not
trap, it's in general more likely to, which can help with debugging.

This will not catch all such errors of course, but it doesn't hurt anything,
except for causing a tiny (probably imperceptible) performance hit, and may
help with debugging.


On the contrary, it _will_ hurt. The programmer will start relying on
the assumption that he has either a null pointer, or a valid one. This
assumption is going to turn around and bite him sooner or later, and
then you try and find where it broke down.

Richard
Nov 15 '05 #16
Flash Gordon wrote:
Bryan Donlan wrote:
Walter Roberson wrote:
In article <43******@212.6 7.96.135>, Bart C <bc@freeuk.co m> wrote:

"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message
news:43******** ********@news.x s4all.nl...
> Writing through a null pointer is no more defined than writing
> through a
> wild pointer.
Maybe, but an error is more or less guaranteed with a null pointer and
makes it easy to check for.
I know of at least two architectures in which NULL is all-zero-bits
but in which writing at (or near that location, such as a[3] when
a is NULL) would NOT result in a trap, because the architectures happen
to have memory at that location.

The vast majority of machines in use have NULL as a trap location.
Richard's
development system likely does so as well. Although *NULL might well not
trap, it's in general more likely to, which can help with debugging.

This will not catch all such errors of course, but it doesn't hurt
anything,
except for causing a tiny (probably imperceptible) performance hit,
and may
help with debugging.

Agreed.

I would also say that even on systems where writing to *NULL does not
trap, it is still likely (but not guaranteed) to give reproduceable
results which can be debugged.

For similar reasons on embedded systems I have changed the startup code
so that all RAM was initialised to all bits 0. I did not check on the
system whether NULL was all bits 0 or whether float or double 0 where
all bits 0, but it did meen the software was more predictable and easier
to debug when accessing uninitialised data. On another system they
initialised all RAM to 0x55 for the similar reason (I used all bits 0
because static variables without an initialiser where *not* set to 0).

IMHO the first stage in trying to debug a problem is always to reproduce
it, since if you can't reliably reproduce it you will never know if you
have fixed what caused the original bug report or something else that
you think might have caused it but did not.

Right.
0x99 (or any with MSBit 1) is a "*_tad_*" (and only) little better..
See Eric SosMan's reply in recent thread of "How to choose between
malloc and calloc"
http://groups.google.com/group/comp....706ab502cce81f

Anand
PS: Is there a better way to point to another thread (not the long
google link way?
Nov 15 '05 #17

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

Similar topics

5
4078
by: Rachel Weeden | last post by:
I'm working on an ASP Web application, and am having syntax issues in a WHERE statement I'm trying to write that uses the CInt Function on a field. Basically, I want to select records using criteria of Race, Gender and Crime Code. But the Crime Code field in the table is text, and I cannot change it. I want to use a range of crime codes, so need to convert it to an integer on-the-fly. Here's what I have in my code so far:
15
3340
by: Viviana Vc | last post by:
How can I programatically do the equivalent of the following: cacls "C:\Program Files\test" /T /G Everyone:f ? Thanks, Viv
2
2306
by: BlackFireNova | last post by:
I have an Access 2003 mdb which contains software records. I need to sort on a particular type of software, and then identify and count how many copies there are per each group of that type purchased on the same date. I have no trouble doing a query to extract the type (say MS Excel 2002, for example). The trouble is, there could be 50 copies purchased on date "x", 80 copies purchased on date "y", and 250 copies purchased on date "z",...
6
1525
by: MathewLovesC | last post by:
Can someone help me out here? I have a bunch of test statements in the program and my test printf("Do I have trouble here after GetNumbers()?\n"); does not display rather I get garbage. Please help. #include <stdio.h> #include <system.h> //prototypes char DisplayTitle(void); void GetNumbers(int *parrNumb);
10
1670
by: Ray Stevens | last post by:
I am attempting to test a VeriSign account and the setup instructions stated that the certs file needed to go into the Windows\System32 folder. I am getting an error from the code-behind assebly stating it cannot find it in the certs chain and is pointing to a path in C:\Program Files\Microsoft Visual Studio 8\Common 7\IDE. Where should I put the certs folder?
77
14455
by: Tark Siala | last post by:
hi i working with TreeView in VB6, and have good Properity Named (Key) with the Key i can goto Any Node i know hes Key. but in VB.NET i can find the Key :( please tell me where i can find the key in TreeView.Net... ----------------------------------------------- Best Regards From Tark
64
7562
by: Bayazee | last post by:
hi can we hide a python code ? if i want to write a commercial software can i hide my source code from users access ? we can conver it to pyc but this file can decompiled ... so ...!! do you have any idea about this ...? --------------------------------------- First Iranian Open Source Community : www.python.ir
3
2256
markmcgookin
by: markmcgookin | last post by:
Hi Folks, I have a VB app, and I have been working at it for a while, and I am now at the stage where I want to create a search function. Now don't be scared! It is in the .Net compact framework, and uses SQL Server CE as the database (This seems to scare off people trying to help! lol) but the connection and reading of data etc is all handled, and I think it is going to be a "relatively" simple function. My database has a number of fields...
0
10643
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...
0
10121
tracyyun
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...
0
9200
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
6881
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
5550
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...
0
5690
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4333
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
3862
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3015
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.