Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 668 Vote(s) - 3.47 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What is the difference between char s[] and char *s?

#1
In C, one can use a string literal in a declaration like this:

char s[] = "hello";

or like this:

char *s = "hello";

So what is the difference? I want to know what actually happens in terms of storage duration, both at compile and run time.
Reply

#2
This declaration:

char s[] = "hello";

Creates *one* object - a `char` array of size 6, called `s`, initialised with the values `'h', 'e', 'l', 'l', 'o', '\0'`. Where this array is allocated in memory, and how long it lives for, depends on where the declaration appears. If the declaration is within a function, it will live until the end of the block that it is declared in, and almost certainly be allocated on the stack; if it's outside a function, it will *probably* be stored within an "initialised data segment" that is loaded from the executable file into writeable memory when the program is run.

On the other hand, this declaration:

char *s ="hello";

Creates *two* objects:

- a **read-only** array of 6 `char`s containing the values `'h', 'e', 'l', 'l', 'o', '\0'`, which has no name and has *static storage duration* (meaning that it lives for the entire life of the program); and
- a variable of type pointer-to-char, called `s`, which is initialised with the location of the first character in that unnamed, read-only array.

The unnamed read-only array is typically located in the "text" segment of the program, which means it is loaded from disk into read-only memory, along with the code itself. The location of the `s` pointer variable in memory depends on where the declaration appears (just like in the first example).
Reply

#3
In the case of:

char *x = "fred";

x is an [lvalue][1] -- it can be assigned to. But in the case of:

char x[] = "fred";

x is not an lvalue, it is an rvalue -- you cannot assign to it.


[1]:

[To see links please register here]

Reply

#4
In the light of comments here it should be obvious that : char * s = "hello" ;
Is a bad idea, and should be used in very narrow scope.

This might be a good opportunity to point out that "const correctness" is a "good thing". Whenever and wherever You can, use the "const" keyword to protect your code, from "relaxed" callers or programmers, which are usually most "relaxed" when pointers come into play.

Enough melodrama, here is what one can achieve when adorning pointers with "const".
(Note: One has to read pointer declarations right-to-left.)
Here are the 3 different ways to protect yourself when playing with pointers :

const DBJ* p means "p points to a DBJ that is const"
— that is, the DBJ object can't be changed via p.

DBJ* const p means "p is a const pointer to a DBJ"
— that is, you can change the DBJ object via p, but you can't change the pointer p itself.

const DBJ* const p means "p is a const pointer to a const DBJ"
— that is, you can't change the pointer p itself, nor can you change the DBJ object via p.

The errors related to attempted const-ant mutations are caught at compile time. There is no runtime space or speed penalty for const.

(Assumption is you are using C++ compiler, of course ?)

--DBJ
Reply

#5
char s[] = "Hello world";

Here, `s` is an array of characters, which can be overwritten if we wish.

char *s = "hello";

A string literal is used to create these character blocks somewhere in the memory which this pointer `s` is pointing to. We can here reassign the object it is pointing to by changing that, but as long as it points to a string literal the block of characters to which it points can't be changed.
Reply

#6
char s[] = "hello";

declares `s` to be an array of `char` which is long enough to hold the initializer (5 + 1 `char`s) and initializes the array by copying the members of the given string literal into the array.

char *s = "hello";

declares `s` to be a pointer to one or more (in this case more) `char`s and points it directly at a fixed (read-only) location containing the literal `"hello"`.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through