Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 246
  • Last Modified:

pointer does not get assigned...

Hi,

I do:

char str[10]="abcd";
char *sptr = str;

sptr[2]='p';

This works...


But

char *sptr = "abcd";
printf("%s\n", sptr[2]);   //This works

sptr[2] = 'p';

This does not work...

I don't get the difference between these 2... they both are pointers and are pointing to a string...

can someone throw some light on this....

Thanks
0
srinivas_vemla
Asked:
srinivas_vemla
3 Solutions
 
Jaime OlivaresSoftware ArchitectCommented:
printf("%s\n", sptr[2]);   //This works

This wouldn't work since printf() is expecting a string and you pass it a character

sptr[2] = 'p';
This may produce a violation because sptr is pointing to a constant string when you declared:
char *sptr = "abcd";
0
 
Sys_ProgCommented:
AFAIK, (or atleast how C++ handles char strings)

when u say
char str[10] = "abcd" ;

A char string of 10 locations is allocated, string abcd is copied into that location
Apart from this, since "abcd" is a string literal,  a separate constant memory is created
Thus, initially, this constant memory is created and then its contents are copied in your str of 10 locations

Thus, sting str itself is not a constant location.
Hence can be changed

Now, when  u say
char *str = "abcd" ;

str is just a pointer pointing to the constant string.
Hence when u try to modiy this location, u get a runtime error because u are modifying a conatnt location


Amit
0
 
srinivas_vemlaAuthor Commented:
Thanks jaime,

Yeah, that was a typo on the printf() line...

So, do you mean that since I assigned sptr to "abcd", I cannot change a single character in it again?... But how come I can change if I declare the string like:

char s[] = "abcd";
s[2] = 'p';  //This works
 
I don't get the difference clearly...
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Sys_ProgCommented:
As I said earlier
When u declare
char s[] = "abcd"

There r 2 steps

1. COnstat string allocation in memmory
2. COpying of this string char-by-char into s (a separarte memory location which is not const)

Thus, u can change s but not the other one

Amit
0
 
AxterCommented:
>>char s[] = "abcd";

In above code s is not pointing to a string literal.  The compiler will allocate memory the size of the string literal, and then copy the contents in the string literal into s

So 's' can be modified, since it's not pointing to a string literal.

char *x = "abcd";

In above line, x is pointing to a string literal.  Changing a string literal is considered undefined behavior according to both C and C++ standards.

Some compilers will let you change it, and some compilers will put the string literal in constant memory, and therefore you'll get a run time error.
When declaring a pointer that points to a string literal, you should always declare it constant, so you can avoid accidently modifying it.

const char* x = "abcd";
0
 
srinivas_vemlaAuthor Commented:
Thanks guys,

That makes it clear...
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now