473,799 Members | 3,858 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 #1
16 1609

mikelinyoho wrote:
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 ); Name is a pointer with no memory associated.Scan f dont allocate
memory.
Change char * name to an array.Use fgets instead of scanf to avoid
overflow issues. title = strcat(name,"th e Great");
printf( "Hello, %s\n", title );
return(0);
}

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

thank you
may goodness be with you all


Nov 15 '05 #2
Do you get compiler errors or what?
Can you be more specific?

Alex.

Nov 15 '05 #3

mikelinyoho wrote:
Regards:

Where the code trouble is?
#include <windows.h>
non standard header
#include <stdio.h>
#include <stdlib.h>
Do you really need this?
#include <string.h>

#define MAXMUNCH ... /* some size big enough for you */
int main(){
int main(void){

is a tad bit better.
char *name;
char *title;
printf( "Enter your name:");
scanf( "%s", name );
Not allocated enough (any) memory -- so you're
dead hereon.

Do name = malloc( sizeof *name * MAXMUNCH ); title = strcat(name,"th e Great");
... and to add insult to injury.
printf( "Hello, %s\n", title );
return(0);
}


Nov 15 '05 #4
mikelinyoho wrote:
Regards:

Where the code trouble is?


The same as in the last 4000 posts with pointers-to-nowhere from
clueless people who refuse to check the FAQ or follow the newsgroup
before posting.
Nov 15 '05 #5
mikelinyoho wrote:

Where the code trouble is?

#include <windows.h>
No such standard header, eliminate it.
#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);
}


Don't use scanf for interactive input. Do fflush of the prompt.
Do read the action of strcat. Do assign memory space to fill. eg:

#include <stdio.h>
#include <string.h>
#define BUFSIZE 80
#define CAPTION " the Great"

int main(void) {
char name[BUFSIZE + 1 + strlen(CAPTION)];
char *p;

printf("Enter your name:);
fflush(stdout);
fgets(name, BUFSIZE, stdin);
if (p = strchr(name, '\n') *p = '\0';
strcat(name, CAPTION");
printf("Hello, %s\n", name);
return(0);
} /* not tested */

Note the test for a final \n in the input and stripping of it.

--
"If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 15 '05 #6

"mikelinyoh o" <mi*********@gm ail.com> wrote in message
news:11******** *************@z 14g2000cwz.goog legroups.com...
Regards:

Where the code trouble is?
.... char *name;
char *title;
printf( "Enter your name:");
scanf( "%s", name );
The compiler might give a warning that 'name' is not initialised.

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), and not zero.

So ideally, there should be (1) a warning (2) 'name' is best set to null
when not initialised, to avoid undefined action, and (3) scanf() should at
very least not assign to a null pointer!
title = strcat(name,"th e Great");


A space might help

Bartthe Great
Nov 15 '05 #7
CBFalconer wrote:
#define BUFSIZE 80
#define CAPTION " the Great"

int main(void) {
char name[BUFSIZE + 1 + strlen(CAPTION)];


Excuse my ignorance, but does it really work? Well, I tried it with gcc and
I see that it really works, but since when?
--
Anton Petrusevich
Nov 15 '05 #8
Anton Petrusevich <ca***@att-ltd.biz> writes:
CBFalconer wrote:
#define BUFSIZE 80
#define CAPTION " the Great"

int main(void) {
char name[BUFSIZE + 1 + strlen(CAPTION)];


Excuse my ignorance, but does it really work? Well, I tried it with gcc and
I see that it really works, but since when?


Variable length arrays (VLAs) are a new feature in C99, not supported
in C90. gcc has supported them (or something very similar) as an
extension for some time. It won't work with a strict C90 compiler.

<OT>
I'm a little surprised to see that gcc doesn't complain about it, even
with "-ansi -pedantic -Wall -W".
</OT>

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #9
Anton Petrusevich wrote:

CBFalconer wrote:
#define BUFSIZE 80
#define CAPTION " the Great"

int main(void) {
char name[BUFSIZE + 1 + strlen(CAPTION)];


Excuse my ignorance, but does it really work? Well, I tried it with gcc and
I see that it really works, but since when?


That should really have been sizeof(CAPTION) to be portable to
C90. Then it doesn't need the +1 either.

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 15 '05 #10

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

Similar topics

5
4076
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
3338
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
2305
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
1524
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
1668
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
14452
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
7554
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
2255
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
9543
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
10488
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
10257
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
10237
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
10029
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
6808
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
5588
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4144
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
3
2941
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.