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

int * vs char *

Hi,

I have a quick question.

The following works:

char *s="hello";
*s="world";

but the following gives a segementation fault:

int *p = 0;
*p = 17;

Could anyone please clarify about this?

yugandhar
Nov 13 '05 #1
3 2449
In 'comp.lang.c', yu*********@yahoo.com wrote:
The following works:
No, it doesn't.
char *s="hello";
*s="world";
Initializing a char ('*s' is 's[0]') with an address is a nonsense and your
compiler should complain about that.

You probably meant:

char *s="hello";
s="world";
but the following gives a segmentation fault:

int *p = 0;
This is eq. to

int *p = NULL;
*p = 17;


Dereferencing the NULL pointer invoques an Undefined Behaviour.

--
-ed- em**********@noos.fr [remove YOURBRA before answering me]
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
<blank line>
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 13 '05 #2

<yu*********@yahoo.com> wrote in message
The following works:

char *s="hello";
*s="world";
This works by chance. The variable s is pointer, i.e. it holds an address.
That address is set to the position of the string literal "hello" in memory.
One of the confusing things about C is that * is used both to declare a
pointer (as in your first line) and to dereference a pointer, or take the
thing it points to.
In your second line you are dereferencing the pointer. The memory location
occupied by the "h" of your string literal is now set to the address of the
string literal "world".
It is probable that on your machine a char occupies 1 byte whilst a char *
occupies 4 bytes. C will cope with this situation by silently discarding the
top three bytes of the address. The 'h' of "hello" is thus set to the lowest
byte of the address of "world". This could be anything.
but the following gives a segementation fault:

int *p = 0;
*p = 17;

Could anyone please clarify about this?

p is a pointer which is set to 0, or NULL, probably physical location 0 in
memory.
*p = 17 writes a 17 to physical location zero in memory. On most machines,
this will trigger some sort of error because location zero is special, and
normal application are not allowed to write to it.
NULL pointers (or p == 0) are used extensively in C to say "this pointer
doesn't point to anything".
Nov 13 '05 #3
On Tue, 22 Jul 2003 23:23:14 +1000, "Simon Biber"
<sb****@optushome.com.au> wrote:
"Malcolm" <ma*****@55bank.freeserve.co.uk> wrote:
<yu*********@yahoo.com> wrote in message
The following works:

char *s="hello";
*s="world";
This works by chance. The variable s is pointer, i.e. it holds an
address. That address is set to the position of the string literal
"hello" in memory.


Works by chance? No, it doesn't work at all. It is an error. Any ANSI C
compiler will complain about the attempt to implicitly convert (char *)
to (char), which is not allowed in C.

Right.

<snip>
In your second line you are dereferencing the pointer. The memory location
occupied by the "h" of your string literal is now set to the address of the
string literal "world".


No, as it is written it has undefined behaviour because of the constraint
violation for the conversion without a cast.


A constraint violation requires a diagnostic. *If* the compiler
continues after that diagnostic, e.g. it is only a warning, then yes
the behavior is not defined by the standard, although any implementor
who does something different for an implicit conversion than the same
explicit (casted) one deserves to be boiled in oil. Repeatedly.
Assuming you specify the
conversion with an explicit cast, it is still undefined behaviour because
it is attempting to modify a string literal.
Right.
Assuming you change it to:
char s[] = "hello";
*s = (char)"world";
Then the address of the string literal "world" is converted to char. If the
value does not fit into the integer type, which is likely here, we still have
undefined behaviour.

No, *that* is an implementation-defined (hence documented) result, or
in C99 only the raising of an implementation-defined signal. Still
not portable, and nowhere near correct, but not the full horror of UB.
- David.Thompson1 at worldnet.att.net
Nov 13 '05 #4

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

Similar topics

9
by: Christopher Benson-Manica | last post by:
I need a smart char * class, that acts like a char * in all cases, but lets you do some std::string-type stuff with it. (Please don't say to use std::string - it's not an option...). This is my...
5
by: Alex Vinokur | last post by:
"Richard Bos" <rlb@hoekstra-uitgeverij.nl> wrote in message news:4180f756.197032434@news.individual.net... to news:comp.lang.c > ben19777@hotmail.com (Ben) wrote: > > 2) Structure casted into an...
5
by: Sona | last post by:
I understand the problem I'm having but am not sure how to fix it. My code passes two char* to a function which reads in some strings from a file and copies the contents into the two char*s. Now...
2
by: Peter Nilsson | last post by:
In a post regarding toupper(), Richard Heathfield once asked me to think about what the conversion of a char to unsigned char would mean, and whether it was sensible to actually do so. And pete has...
5
by: jab3 | last post by:
(again :)) Hello everyone. I'll ask this even at risk of being accused of not researching adequately. My question (before longer reasoning) is: How does declaring (or defining, whatever) a...
12
by: GRoll35 | last post by:
I get 4 of those errors. in the same spot. I'll show my parent class, child class, and my driver. All that is suppose to happen is the user enters data and it uses parent/child class to display...
18
by: Pedro Pinto | last post by:
Hi there once more........ Instead of showing all the code my problem is simple. I've tried to create this function: char temp(char *string){ alterString(string); return string;
4
by: Paul Brettschneider | last post by:
Hello all, consider the following code: typedef char T; class test { T *data; public: void f(T, T, T); void f2(T, T, T);
16
by: s0suk3 | last post by:
This code #include <stdio.h> int main(void) { int hello = {'h', 'e', 'l', 'l', 'o'}; char *p = (void *) hello; for (size_t i = 0; i < sizeof(hello); ++i) {
29
by: Kenzogio | last post by:
Hi, I have a struct "allmsg" and him member : unsigned char card_number; //16 allmsg.card_number
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...

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.