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

understanding pointers

Can you please explain the below pointer initializations:

char *cptr = "test"; /* gives no error and warnings */
int *iptr = 10; /* gives only warning */
float *fptr = 3.14; /* gives error */

Why?

Thanks
Jun 27 '08 #1
26 1427
sumsin wrote:
Can you please explain the below pointer initializations:

char *cptr = "test"; /* gives no error and warnings */
Because there no error and nothing to warn about.
cptr now points to the (read-only) first character of the charachter array
"test"
int *iptr = 10; /* gives only warning */
Because 10 potentially could be an address, but most probably is not, at
least none from the address space you program has been given by the OS.
You can inititialize an int with 10, but should inititialize an in * with
the address of an int
float *fptr = 3.14; /* gives error */
Because an address can't be a floating point value
You can initialite a float with 3.14, but not a float *, here you'd need the
address of a float.
>
Why?

Thanks
Bye, Jojo
Jun 27 '08 #2
sumsin <su*******@gmail.comwrites:
Can you please explain the below pointer initializations:

char *cptr = "test"; /* gives no error and warnings */
"test" is a string literal, an expression of array type. In most
contexts, including this one, an expression of array type is
implicitly converted to a pointer to the array's first element.

It would be better to declare
const char *cptr = "test";
You're not allowed to modify the contents of a string literal
(attempting to do so is undefined behavior), but for historical
reasons string literals are not const.

See section 6 of the comp.lang.c FAQ, <http://www.c-faq.com>.
int *iptr = 10; /* gives only warning */
This is actually a constraint violation; the compiler is free to
reject it rather than just print a warning. This doesn't cause iptr
to point to an int object with the value 10; it attempts to use the
value 10 (of type int) to initialize an object of type int*. Your
compiler is probably letting you get away with treating 10 as an
address, implicitly converted from int to int*.

If you really wanted to do that, you could write:
int *iptr = (int*)10;
but its not likely to be meaningful.

If you want iptr to point to an object with the value 10, you're going
to have to create such an object:

int obj = 10;
int *iptr = &obj;
float *fptr = 3.14; /* gives error */
This attempts to assign a value of type double to an object of type
float*. You can't do that. If you want ftpr to point to a float
object with the value 3.14, you'll have to create such an object, as
above.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #3
On May 29, 11:12 am, "Joachim Schmitz" <nospam.j...@schmitz-
digital.dewrote:
sumsin wrote:
Can you please explain the below pointer initializations:
char *cptr = "test"; /* gives no error and warnings */

Because there no error and nothing to warn about.
cptr now points to the (read-only) first character of the character array
"test"
So what will be the value of cptr? Will it be an address kind of value
or the lateral string ("test") itself?
Because when I try to print the different values like:

printf("\n&cptr = %p", &cptr); /* prints the address of cptr */
&cptr = 0x7fbffff938

printf("\ncptr = %p", cptr); /* prints value of cptr */
cptr = 0x4005ec

but when I try like this:
printf("\ncptr = %s", cptr); /* prints value of cptr */
cptr = test

printf("\ncptr = %s", *cptr); /* prints value at cptr */
Segmentation fault
cptr (name)
-------------------
| What should come |
| here? | <- (This should be some address kind of vale,
right?)
| |
-------------------
0x7fbffff938 (Address of cptr)
Jun 27 '08 #4
sumsin wrote:
On May 29, 11:12 am, "Joachim Schmitz" <nospam.j...@schmitz-
digital.dewrote:
>sumsin wrote:
>>Can you please explain the below pointer initializations:
>>char *cptr = "test"; /* gives no error and warnings */

Because there no error and nothing to warn about.
cptr now points to the (read-only) first character of the character
array "test"
So what will be the value of cptr? Will it be an address kind of value
or the lateral string ("test") itself?
The address of the 1st character (the 1st 't') of the text literal ("test")
Because when I try to print the different values like:

printf("\n&cptr = %p", &cptr); /* prints the address of cptr */
&cptr = 0x7fbffff938
That's the address of the pointer
printf("\ncptr = %p", cptr); /* prints value of cptr */
cptr = 0x4005ec
This is the value of the pointer, i.e. the address of the 1st character (the
1st 't') of the text literal ("test")
but when I try like this:
printf("\ncptr = %s", cptr); /* prints value of cptr */
cptr = test
Indeed. printf's %s prints the \0 termitated character array that start at
cprt.
printf("\ncptr = %s", *cptr); /* prints value at cptr */
Segmentation fault
cptr (name)
-------------------
>What should come |
here? | <- (This should be some address kind of vale,
right?) |
-------------------
0x7fbffff938 (Address of cptr)

Jun 27 '08 #5
"sumsin" <su*******@gmail.comwrote in message
news:cc**********************************@y22g2000 prd.googlegroups.com...
On May 29, 11:12 am, "Joachim Schmitz" <nospam.j...@schmitz-
digital.dewrote:
>sumsin wrote:
Can you please explain the below pointer initializations:
char *cptr = "test"; /* gives no error and warnings */

Because there no error and nothing to warn about.
cptr now points to the (read-only) first character of the character array
"test"
So what will be the value of cptr? Will it be an address kind of value
or the lateral string ("test") itself?
Because when I try to print the different values like:

printf("\n&cptr = %p", &cptr); /* prints the address of cptr */
FWIW, this is undefined behavior. You need to ensure that your passing a
single void* type for every %p formatter:

printf("\n&cptr = %p", (void*)&cptr);
[...]

Jun 27 '08 #6
Dan

"sumsin" <su*******@gmail.comwrote in message
news:ee**********************************@c19g2000 prf.googlegroups.com...
Can you please explain the below pointer initializations:

char *cptr = "test"; /* gives no error and warnings */
A pointer that points to the start of a string.
int *iptr = 10; /* gives only warning */
A pointer pointing to address 10
float *fptr = 3.14; /* gives error */
No such thing as a floating address.
Why?
If you want to point to a value, you first need a variable to hold that
value.

float f=3.14;
float *fptr = &f;

Your confusion probably comes from the char *cptr = "test". C does special
things with array's. They are not like other data types, whats its sort of
really doing is:
char c[5] = "test";
char *cptr = &c[0]; (which is the same as char *cptr = c;)


Jun 27 '08 #7
On May 29, 11:54 am, "Joachim Schmitz" <nospam.j...@schmitz-
digital.dewrote:
sumsin wrote:
On May 29, 11:12 am, "Joachim Schmitz" <nospam.j...@schmitz-
digital.dewrote:
sumsin wrote:
Can you please explain the below pointer initializations:
>char *cptr = "test"; /* gives no error and warnings */
Because there no error and nothing to warn about.
cptr now points to the (read-only) first character of the character
array "test"
So what will be the value of cptr? Will it be an address kind of value
or the lateral string ("test") itself?

The address of the 1st character (the 1st 't') of the text literal ("test")
printf("\ncptr = %p", cptr); /* prints value of cptr */
cptr = 0x4005ec

This is the value of the pointer, i.e. the address of the 1st character (the
1st 't') of the text literal ("test")but when I try like this:
printf("\ncptr = %s", cptr); /* prints value of cptr */
cptr = test

Indeed. printf's %s prints the \0 termitated character array that start at
cprt.
You mean cptr will contain the address of the 1st character of the
text literal "test".
Something like:
cptr = 0x4005ec (is it ok?)
Is it the address of 1st character of the text literal "test"?

Then when I try to dereference(*cptr) this pointer then why do I go
that "Segmentation fault"?

Jun 27 '08 #8
sumsin wrote:
On May 29, 11:54 am, "Joachim Schmitz" <nospam.j...@schmitz-
digital.dewrote:
>sumsin wrote:
On May 29, 11:12 am, "Joachim Schmitz" <nospam.j...@schmitz-
digital.dewrote:
sumsin wrote:
Can you please explain the below pointer initializations:
>>char *cptr = "test"; /* gives no error and warnings */
>Because there no error and nothing to warn about.
cptr now points to the (read-only) first character of the
character array "test"
So what will be the value of cptr? Will it be an address kind of
value or the lateral string ("test") itself?

The address of the 1st character (the 1st 't') of the text literal
("test")
printf("\ncptr = %p", cptr); /* prints value of cptr */
cptr = 0x4005ec

This is the value of the pointer, i.e. the address of the 1st
character (the 1st 't') of the text literal ("test")but when I try
like this:
printf("\ncptr = %s", cptr); /* prints value of cptr */
cptr = test

Indeed. printf's %s prints the \0 termitated character array that
start at cprt.

You mean cptr will contain the address of the 1st character of the
text literal "test".
Something like:
cptr = 0x4005ec (is it ok?)
Is it the address of 1st character of the text literal "test"?

Then when I try to dereference(*cptr) this pointer then why do I go
that "Segmentation fault"?
It's undefined behaviour to modify string literals. cptr points to one
and your compiler might have put in read-only memory.

Jun 27 '08 #9
On May 29, 2:07 pm, santosh <santosh....@gmail.comwrote:
sumsin wrote:
On May 29, 11:54 am, "Joachim Schmitz" <nospam.j...@schmitz-
digital.dewrote:
sumsin wrote:
On May 29, 11:12 am, "Joachim Schmitz" <nospam.j...@schmitz-
digital.dewrote:
sumsin wrote:
Can you please explain the below pointer initializations:
>char *cptr = "test"; /* gives no error and warnings */
Because there no error and nothing to warn about.
cptr now points to the (read-only) first character of the
character array "test"
So what will be the value of cptr? Will it be an address kind of
value or the lateral string ("test") itself?
The address of the 1st character (the 1st 't') of the text literal
("test")
printf("\ncptr = %p", cptr); /* prints value of cptr */
cptr = 0x4005ec
This is the value of the pointer, i.e. the address of the 1st
character (the 1st 't') of the text literal ("test")but when I try
like this:
printf("\ncptr = %s", cptr); /* prints value of cptr */
cptr = test
Indeed. printf's %s prints the \0 termitated character array that
start at cprt.
You mean cptr will contain the address of the 1st character of the
text literal "test".
Something like:
cptr = 0x4005ec (is it ok?)
Is it the address of 1st character of the text literal "test"?
Then when I try to dereference(*cptr) this pointer then why do I go
that "Segmentation fault"?

It's undefined behavior to modify string literals. cptr points to one
and your compiler might have put in read-only memory.
You mean compiler implicitly put string literal "test" at some read
only memory (is it un-named?) and to de-reference this memory is a
segmentation violation, is it?
Jun 27 '08 #10

"sumsin" <su*******@gmail.comwrote in message
news:ee**********************************@c19g2000 prf.googlegroups.com...
Can you please explain the below pointer initializations:

char *cptr = "test"; /* gives no error and warnings */
int *iptr = 10; /* gives only warning */
float *fptr = 3.14; /* gives error */

Why?
'Why' has been explained. But I think rewriting like this gives the effect
you want:

char *cptr = "test";
int iptr[] = {10};
float fptr[] = {3.14};

Access the values pointed to as *iptr and *fptr.

--
Bartc
Jun 27 '08 #11
On Thu, 29 May 2008 02:03:39 -0700 (PDT), sumsin <su*******@gmail.com>
wrote:
>On May 29, 11:54 am, "Joachim Schmitz" <nospam.j...@schmitz-
digital.dewrote:
>sumsin wrote:
On May 29, 11:12 am, "Joachim Schmitz" <nospam.j...@schmitz-
digital.dewrote:
sumsin wrote:
Can you please explain the below pointer initializations:
>>char *cptr = "test"; /* gives no error and warnings */
>Because there no error and nothing to warn about.
cptr now points to the (read-only) first character of the character
array "test"
So what will be the value of cptr? Will it be an address kind of value
or the lateral string ("test") itself?

The address of the 1st character (the 1st 't') of the text literal ("test")
printf("\ncptr = %p", cptr); /* prints value of cptr */
cptr = 0x4005ec

This is the value of the pointer, i.e. the address of the 1st character (the
1st 't') of the text literal ("test")but when I try like this:
printf("\ncptr = %s", cptr); /* prints value of cptr */
cptr = test

Indeed. printf's %s prints the \0 termitated character array that start at
cprt.

You mean cptr will contain the address of the 1st character of the
text literal "test".
Something like:
cptr = 0x4005ec (is it ok?)
Is it the address of 1st character of the text literal "test"?

Then when I try to dereference(*cptr) this pointer then why do I go
that "Segmentation fault"?
You need to show how you dereference it. There are lots of wrong ways
to attempt to dereference cptr.

If you tried to change the value pointed to, as in
*cptr = 'w';
then you invoked undefined behavior by attempting to modify a string
literal.

If you attempted to print it as if it were a string, as in
printf("\ncptr = %s", *cptr);
then you invoked undefined behavior by passing the wrong argument type
to printf.

A segmentation fault is one of the best kinds of undefined behavior.
It quickly eliminates any false hope that your code is correct.

Show us what you did.
Remove del for email
Jun 27 '08 #12
On Thu, 29 May 2008 03:07:14 -0700 (PDT), sumsin <su*******@gmail.com>
wrote:
>On May 29, 2:07 pm, santosh <santosh....@gmail.comwrote:
>sumsin wrote:
On May 29, 11:54 am, "Joachim Schmitz" <nospam.j...@schmitz-
digital.dewrote:
sumsin wrote:
On May 29, 11:12 am, "Joachim Schmitz" <nospam.j...@schmitz-
digital.dewrote:
sumsin wrote:
Can you please explain the below pointer initializations:
>>char *cptr = "test"; /* gives no error and warnings */
>Because there no error and nothing to warn about.
cptr now points to the (read-only) first character of the
character array "test"
So what will be the value of cptr? Will it be an address kind of
value or the lateral string ("test") itself?
>The address of the 1st character (the 1st 't') of the text literal
("test")
printf("\ncptr = %p", cptr); /* prints value of cptr */
cptr = 0x4005ec
>This is the value of the pointer, i.e. the address of the 1st
character (the 1st 't') of the text literal ("test")but when I try
like this:
printf("\ncptr = %s", cptr); /* prints value of cptr */
cptr = test
>Indeed. printf's %s prints the \0 termitated character array that
start at cprt.
You mean cptr will contain the address of the 1st character of the
text literal "test".
Something like:
cptr = 0x4005ec (is it ok?)
Is it the address of 1st character of the text literal "test"?
Then when I try to dereference(*cptr) this pointer then why do I go
that "Segmentation fault"?

It's undefined behavior to modify string literals. cptr points to one
and your compiler might have put in read-only memory.

You mean compiler implicitly put string literal "test" at some read
only memory (is it un-named?) and to de-reference this memory is a
segmentation violation, is it?
Memory doesn't have names. But you could consider the string literal
an anonymous array (one without a name). Whether you system stores
literals in read only memory is an implementation specific detail
which has no impact on the fact that you are not allowed to modify a
string literal. If you have read only memory, dereferencing an
address to this memory is only illegal if you attempt to modify the
data at that address.
Remove del for email
Jun 27 '08 #13
sumsin wrote:
On May 29, 11:54 am, "Joachim Schmitz" <nospam.j...@schmitz-
digital.dewrote:
>sumsin wrote:
>>On May 29, 11:12 am, "Joachim Schmitz" <nospam.j...@schmitz-
digital.dewrote:
sumsin wrote:
Can you please explain the below pointer initializations:
char *cptr = "test"; /* gives no error and warnings */
You mean cptr will contain the address of the 1st character of the
text literal "test".
Yes.

/* BEGIN new.c */

#include <stdio.h>

int main(void)
{
char *cptr = "test";

do {
printf("%c", *cptr);
} while(*cptr++ != '\0');
printf("\n");
return 0;
}

/* END new.c */
--
pete
Jun 27 '08 #14
On May 29, 4:42 pm, Barry Schwarz <schwa...@dqel.comwrote:
On Thu, 29 May 2008 02:03:39 -0700 (PDT), sumsin <sumsin...@gmail.com>
wrote:
On May 29, 11:54 am, "Joachim Schmitz" <nospam.j...@schmitz-
digital.dewrote:
sumsin wrote:
On May 29, 11:12 am, "Joachim Schmitz" <nospam.j...@schmitz-
digital.dewrote:
sumsin wrote:
Can you please explain the below pointer initializations:
>char *cptr = "test"; /* gives no error and warnings */
Because there no error and nothing to warn about.
cptr now points to the (read-only) first character of the character
array "test"
So what will be the value of cptr? Will it be an address kind of value
or the lateral string ("test") itself?
The address of the 1st character (the 1st 't') of the text literal ("test")
printf("\ncptr = %p", cptr); /* prints value of cptr */
cptr = 0x4005ec
This is the value of the pointer, i.e. the address of the 1st character (the
1st 't') of the text literal ("test")but when I try like this:
printf("\ncptr = %s", cptr); /* prints value of cptr */
cptr = test
Indeed. printf's %s prints the \0 termitated character array that start at
cprt.
You mean cptr will contain the address of the 1st character of the
text literal "test".
Something like:
cptr = 0x4005ec (is it ok?)
Is it the address of 1st character of the text literal "test"?
Then when I try to dereference(*cptr) this pointer then why do I go
that "Segmentation fault"?

You need to show how you dereference it. There are lots of wrong ways
to attempt to dereference cptr.

If you tried to change the value pointed to, as in
*cptr = 'w';
then you invoked undefined behavior by attempting to modify a string
literal.

If you attempted to print it as if it were a string, as in
printf("\ncptr = %s", *cptr);
then you invoked undefined behavior by passing the wrong argument type
to printf.

A segmentation fault is one of the best kinds of undefined behavior.
It quickly eliminates any false hope that your code is correct.

Show us what you did.

Remove del for email
I am using:
printf("\ncptr = %s", *cptr);
Jun 27 '08 #15
sumsin wrote:
>>>>>>Can you please explain the below pointer initializations:
>>char *cptr = "test"; /* gives no error and warnings */
I am using:
printf("\ncptr = %s", *cptr);
Any of these, will do something, correctly:

printf("cptr = %p\n", (void *)cptr);
printf("cptr = %s\n", cptr);
printf("cptr = %c\n", *cptr);

I'm guessing the first one is closest to your intent.

--
pete
Jun 27 '08 #16
"Dan" <vo***@sometwher.worldwrites:
"sumsin" <su*******@gmail.comwrote in message
news:ee**********************************@c19g2000 prf.googlegroups.com...
>Can you please explain the below pointer initializations:

char *cptr = "test"; /* gives no error and warnings */
A pointer that points to the start of a string.
>int *iptr = 10; /* gives only warning */
A pointer pointing to address 10
Not necessarily. The declaration violates a constraint; you can't
legally assign an int value to an object of type int* (other than the
special case of a constant 0, a null pointer constant).

It's very likely that, if the compiler accepts this (after issuing
the required diagnostic), it's going to implicitly convert the value
10 to int*, as if you had written
int *iptr = (int*)10;
but there's no requirement for it to do so; once a constraint is
violated, the behavior is undefined.
>float *fptr = 3.14; /* gives error */
No such thing as a floating address.
Right -- and there's no such thing as an integer address either. (You
can *convert* integers to pointers, but only explicitly.)

<OT>
The old VAXC compiler, if I recall correctly, would allow
double *fptr = &3.14;
</OT>

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #17
sumsin <su*******@gmail.comwrites:
On May 29, 11:54 am, "Joachim Schmitz" <nospam.j...@schmitz-
digital.dewrote:
[...]
>sumsin wrote:
Can you please explain the below pointer initializations:
>>char *cptr = "test"; /* gives no error and warnings */
[...]
You mean cptr will contain the address of the 1st character of the
text literal "test".
Right.
Something like:
cptr = 0x4005ec (is it ok?)
Not quite. Pointers are not integers.
Is it the address of 1st character of the text literal "test"?
Yes.
Then when I try to dereference(*cptr) this pointer then why do I go
that "Segmentation fault"?
You don't. Here's what you did:
| printf("\ncptr = %s", *cptr); /* prints value at cptr */
| Segmentation fault

The dereference was fine; it yield the value 't', of type char. You
got a segmentation fault when you tried to pass that char value to
printf with a "%s" format, which caused it to expect a char* value.

You can't *modify* a string literal, but you can read it. And you can
(and should) get the compiler to complain if you try to modify the
string literal by using "const":

const char *cptr = "test";

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #18
"Bartc" <bc@freeuk.comwrites:
"sumsin" <su*******@gmail.comwrote in message
news:ee**********************************@c19g2000 prf.googlegroups.com...
>Can you please explain the below pointer initializations:

char *cptr = "test"; /* gives no error and warnings */
int *iptr = 10; /* gives only warning */
float *fptr = 3.14; /* gives error */

Why?

'Why' has been explained. But I think rewriting like this gives the effect
you want:

char *cptr = "test";
int iptr[] = {10};
float fptr[] = {3.14};

Access the values pointed to as *iptr and *fptr.
Right, but iptr and fptr are now arrays. This means, for example,
that you can't modify them to point somewhere else (since they don't
actually point anywhere in the first place).

As always, see section 6 of the comp.lang.c FAQ.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #19

"Keith Thompson" <ks***@mib.orgwrote in message
news:ln************@nuthaus.mib.org...
"Bartc" <bc@freeuk.comwrites:
>"sumsin" <su*******@gmail.comwrote in message
news:ee**********************************@c19g200 0prf.googlegroups.com...
>>Can you please explain the below pointer initializations:

char *cptr = "test"; /* gives no error and warnings */
int *iptr = 10; /* gives only warning */
float *fptr = 3.14; /* gives error */

Why?

'Why' has been explained. But I think rewriting like this gives the
effect
you want:

char *cptr = "test";
int iptr[] = {10};
float fptr[] = {3.14};

Access the values pointed to as *iptr and *fptr.

Right, but iptr and fptr are now arrays. This means, for example,
that you can't modify them to point somewhere else (since they don't
actually point anywhere in the first place).
You're completely right. I just wanted to initialise iptr and fptr in one
line like cptr.

Looks like a string literal is a very special case of initialising a pointer
then? That makes things awkward:

int *iptr = (int*)"\012\0\0";

--
bartc
Jun 27 '08 #20
"Bartc" <bc@freeuk.comwrites:
"Keith Thompson" <ks***@mib.orgwrote in message
news:ln************@nuthaus.mib.org...
>"Bartc" <bc@freeuk.comwrites:
<snip>
>>you want:

char *cptr = "test";
int iptr[] = {10};
float fptr[] = {3.14};

Access the values pointed to as *iptr and *fptr.

Right, but iptr and fptr are now arrays.
<snip>
Looks like a string literal is a very special case of initialising a pointer
then? That makes things awkward:

int *iptr = (int*)"\012\0\0";
Quite apart from the portability of strings contents, the pointer is
not guaranteed to be aligned for an int. Anyway, no need. String
literals are no longer that special. C has compound literals like
this:

int *ip = (int []){10};
float *fp = (float []){3.14};

--
Ben.
Jun 27 '08 #21
On Thu, 29 May 2008 16:28:47 +0000, Bartc wrote:
Looks like a string literal is a very special case of initialising a
pointer then?
A string literal is a very special case of initialising an array, but when
used to as a pointer initialiser, there is not much special about it. A
string literal is an array of char, which in most contexts is implicitly
converted to a pointer to the initial element. It works like that for
arrays of other types too.
That makes things awkward:

int *iptr = (int*)"\012\0\0";
In C99, you can do this:
int *iptr = (int[]){ 10 };
Jun 27 '08 #22
On Thu, 29 May 2008 05:35:33 -0700 (PDT), sumsin <su*******@gmail.com>
wrote:
>On May 29, 4:42 pm, Barry Schwarz <schwa...@dqel.comwrote:
>On Thu, 29 May 2008 02:03:39 -0700 (PDT), sumsin <sumsin...@gmail.com>
wrote:
>On May 29, 11:54 am, "Joachim Schmitz" <nospam.j...@schmitz-
digital.dewrote:
sumsin wrote:
On May 29, 11:12 am, "Joachim Schmitz" <nospam.j...@schmitz-
digital.dewrote:
sumsin wrote:
Can you please explain the below pointer initializations:
>>char *cptr = "test"; /* gives no error and warnings */
>Because there no error and nothing to warn about.
cptr now points to the (read-only) first character of the character
array "test"
So what will be the value of cptr? Will it be an address kind of value
or the lateral string ("test") itself?
>The address of the 1st character (the 1st 't') of the text literal ("test")
printf("\ncptr = %p", cptr); /* prints value of cptr */
cptr = 0x4005ec
>This is the value of the pointer, i.e. the address of the 1st character (the
1st 't') of the text literal ("test")but when I try like this:
printf("\ncptr = %s", cptr); /* prints value of cptr */
cptr = test
>Indeed. printf's %s prints the \0 termitated character array that start at
cprt.
>You mean cptr will contain the address of the 1st character of the
text literal "test".
Something like:
cptr = 0x4005ec (is it ok?)
Is it the address of 1st character of the text literal "test"?
>Then when I try to dereference(*cptr) this pointer then why do I go
that "Segmentation fault"?

You need to show how you dereference it. There are lots of wrong ways
to attempt to dereference cptr.

If you tried to change the value pointed to, as in
*cptr = 'w';
then you invoked undefined behavior by attempting to modify a string
literal.

If you attempted to print it as if it were a string, as in
printf("\ncptr = %s", *cptr);
then you invoked undefined behavior by passing the wrong argument type
to printf.

A segmentation fault is one of the best kinds of undefined behavior.
It quickly eliminates any false hope that your code is correct.

Show us what you did.

Remove del for email

I am using:
printf("\ncptr = %s", *cptr);
What type of argument does %s require? What is the type of *cptr that
you actually provide? Does the actual type match the required type?
(Obviously not but do you see why?)
Remove del for email
Jun 27 '08 #23
On Thu, 29 May 2008 07:25:27 -0500, pete <pf*****@mindspring.com>
wrote:
>sumsin wrote:
>On May 29, 11:54 am, "Joachim Schmitz" <nospam.j...@schmitz-
digital.dewrote:
>>sumsin wrote:
On May 29, 11:12 am, "Joachim Schmitz" <nospam.j...@schmitz-
digital.dewrote:
sumsin wrote:
>Can you please explain the below pointer initializations:
>char *cptr = "test"; /* gives no error and warnings */
>You mean cptr will contain the address of the 1st character of the
text literal "test".

Yes.

/* BEGIN new.c */

#include <stdio.h>

int main(void)
{
char *cptr = "test";

do {
printf("%c", *cptr);
} while(*cptr++ != '\0');
You probably meant *++cptr to avoid sending the '\0' through printf.
printf("\n");
return 0;
}

/* END new.c */

Remove del for email
Jun 27 '08 #24
Barry Schwarz wrote:
On Thu, 29 May 2008 07:25:27 -0500, pete <pf*****@mindspring.com>
wrote:
>sumsin wrote:
>>On May 29, 11:54 am, "Joachim Schmitz" <nospam.j...@schmitz-
digital.dewrote:
sumsin wrote:
On May 29, 11:12 am, "Joachim Schmitz" <nospam.j...@schmitz-
digital.dewrote:
>sumsin wrote:
>>Can you please explain the below pointer initializations:
>>char *cptr = "test"; /* gives no error and warnings */
You mean cptr will contain the address of the 1st character of the
text literal "test".
Yes.

/* BEGIN new.c */

#include <stdio.h>

int main(void)
{
char *cptr = "test";

do {
printf("%c", *cptr);
} while(*cptr++ != '\0');

You probably meant *++cptr to avoid sending the '\0' through printf.
I hadn't thought of it at the time.
But for text streams, I do think it is better
to only write printing characters, as you suggest.
>
> printf("\n");
return 0;
}

/* END new.c */

--
pete
Jun 27 '08 #25
pete wrote:
Barry Schwarz wrote:
>pete <pf*****@mindspring.comwrote:
.... snip ...
>>
>>#include <stdio.h>

int main(void) {
char *cptr = "test";

do {
printf("%c", *cptr);
} while(*cptr++ != '\0');

You probably meant *++cptr to avoid sending the '\0' through printf.

I hadn't thought of it at the time. But for text streams, I do
think it is better to only write printing characters, as you suggest.
Isn't the loop:

while (*cptr) putchar(*cptr++);

slightly simpler?

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
** Posted from http://www.teranews.com **
Jun 27 '08 #26
CBFalconer wrote:
pete wrote:
>Barry Schwarz wrote:
>>pete <pf*****@mindspring.comwrote:
... snip ...
>>>#include <stdio.h>

int main(void) {
char *cptr = "test";

do {
printf("%c", *cptr);
} while(*cptr++ != '\0');
You probably meant *++cptr to avoid sending the '\0' through printf.
I hadn't thought of it at the time. But for text streams, I do
think it is better to only write printing characters, as you suggest.

Isn't the loop:

while (*cptr) putchar(*cptr++);

slightly simpler?
Yes.
I had a feeling that putchar might be a new concept for OP,
which I didn't feel like introducing.
But your while loop is much more appropriate than my do loop,
for what I was trying to show.

--
pete
Jun 27 '08 #27

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

Similar topics

9
by: deancoo | last post by:
Hello all, I'm new to the C++ STL, and am trying to wrap my head around why the following piece of code doesn't compile. What am I doing wrong? Thanks for any help. d #include <cstdlib>...
26
by: Materialised | last post by:
Hi everyone, I seen the post by Rob Morris, and thought that I would double check that I was using pointers in the correct way. So I written the following string functions to test. I know soem can...
3
by: dj | last post by:
I've read section 6 of the FAQ, but still am a bit confused... Please refer to the comments in the following code for my questions: -----------------------------------------------------------...
82
by: Eric Lindsay | last post by:
I have been trying to get a better understanding of simple HTML, but I am finding conflicting information is very common. Not only that, even in what seemed elementary and without any possibility...
18
by: Simon | last post by:
Hi, I understand what one the differences between std::vector, std::deque and std::list is, the std::vector can have data inserted/deleted at the end. The std::deque can have data...
17
by: Henning Hasemann | last post by:
I have a function which gets the adress of an object as argument. It does some comparsion with the object's contents and then returns. no Reference or pointer to the object is stored or will be...
9
by: igor.kulkin | last post by:
References is a relatively basic feature of C++ language. It might be a good thing to think of references as aliases to the variables. However it's good to think of references this way when you...
5
by: arnuld | last post by:
it works fine: /* C++ Primer - 4/e * * example from section 7.2.2, pointer-swap * STATEMENT * in a function call where parameters are pointers, we actually copy the pointers. * here in this...
13
by: Sri Harsha Dandibhotla | last post by:
Hello all. I recently came across a function declaration as : char(*(*x()))(); This was not in some code but it was in a C questions thread on some group. I tried to decipher what it returns but...
12
by: kevin.eugene08 | last post by:
Hello all, Forgive the somewhat simple question I am sure this will be, but I am having some problem understanding what: char **argv looks like. For instance, i know through trial and error...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...

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.