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

Why is this simple program not working?

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include<stdio.h>
  3.  
  4. int main()
  5. {
  6.     char *s="hello";
  7.     *(s+2)='a';
  8.     printf("%s",s);
  9.     return 0;
  10. }
  11.  
it gives a runtime error, however if i declare s as a character array like s[]="hello", it works, why?
Apr 9 '12 #1

✓ answered by weaknessforcats

Also,

Expand|Select|Wrap|Line Numbers
  1. char *s="hello";
says s is the address of the h, which is const. It's const becuse "hello" is a const array since it is a literal.

When you try to change this literal at run time you crash trying to change a const value.

You may do as manontheedge recommends rvided you delete the array.

If you need a local variable cod it this way:

Expand|Select|Wrap|Line Numbers
  1. char s[] ="hello";
Now this says that s is a char array allocated on the stack and initialized to hello+\0. The array has 6 elements.

2 1428
manontheedge
175 100+
the problem is in the line

Expand|Select|Wrap|Line Numbers
  1. char *s="hello";
when you create a dynamic array, as you have done with

Expand|Select|Wrap|Line Numbers
  1. char* s
you need to reserve space in memory for it. You do that like this ...

Expand|Select|Wrap|Line Numbers
  1. char* s = new char[sizeof("hello")];
then, you can't assign a string of characters ("hello") to a dynamic char array using the equals sign. You have to copy the characters like this

Expand|Select|Wrap|Line Numbers
  1. strcpy(s, "hello");
... should work aside from that
Apr 10 '12 #2
weaknessforcats
9,208 Expert Mod 8TB
Also,

Expand|Select|Wrap|Line Numbers
  1. char *s="hello";
says s is the address of the h, which is const. It's const becuse "hello" is a const array since it is a literal.

When you try to change this literal at run time you crash trying to change a const value.

You may do as manontheedge recommends rvided you delete the array.

If you need a local variable cod it this way:

Expand|Select|Wrap|Line Numbers
  1. char s[] ="hello";
Now this says that s is a char array allocated on the stack and initialized to hello+\0. The array has 6 elements.
Apr 10 '12 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

38
by: jrlen balane | last post by:
basically what the code does is transmit data to a hardware and then receive data that the hardware will transmit. import serial import string import time from struct import * ser =...
3
by: David | last post by:
i am trying to compile the following with gcc on a RH 9.0 box: #include <sys/types.h> #include <sys/resource.h> #include <sys/time.h> #include <unistd.h> #include <stdio.h> /* resource.c */
1
by: steve smith | last post by:
Hi I have just downloaded the Borland C# Builder and the Micorsoft ..Net framework SDK v1.1 from the borland webist, and i am trying to get a simple program to run, however I keep getting errors,...
6
by: academic | last post by:
I want to compile a simple c program. Not c++ Not even Windows. Just a simple program to run in a DOS window. In vs2005 what type of project to I use. I can't get started. I tried a couple...
1
by: merdanus | last post by:
hello i'm trying to use assembly in c and other hll and decided to start with pascal.what's wrong with the program below.it has syntax error in .model small and so on Uses crt; var ...
10
by: Ron | last post by:
I want to calculate the surface area of a sphere from an inputed radius with option strict on. I guess I am not converting something correctly. Here is what I am doing: I have a textbox...
4
by: Break2 | last post by:
I am trying to write a simple program which asks a user to enter 5 integers after which the average will be computed and written to the screen. That simple. However I want to do it according to a...
2
by: zensunni | last post by:
Here's the simple code: #include <windows.h> int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { MessageBox(NULL, "Goodbye, cruel...
5
by: Break2 | last post by:
I am new to C++ and have learned that when I build a program and I want to learn how, it is best to build each program according to the same standard. In my programs I want to have a function...
4
by: Villanmac | last post by:
Ahhhh, ive missed so many lessons this year that i have to try and teach myself c++ from a book and its so hard. Usually I just read this forums to pick things up but its doing my head in trying to...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
0
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...
0
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...
0
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,...

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.