Hakan
Hakan

Reputation: 109

Assignment a value to index of pointer

I try to assign a value to the second index of pointer, it gives me a warning

"[Warning] assignment makes integer from pointer without a cast"

and it doesn't run this program. I wonder, where am I making a mistake?

#include<stdio.h>

void main()
{
    char *p="John";
    *(p+2)="v";
    printf("%s",p);
}

Upvotes: 0

Views: 2066

Answers (3)

John Bode
John Bode

Reputation: 123558

You're making two mistakes.

First, you are attempting to modify the contents of a string literal; this invokes undefined behavior, meaning any of the following are possible (and considered equally correct): your code may crash, it may run to completion with no issues, it may run to completion but not alter the string literal, it may leave your system in a bad state, etc. Your compiler may reject the code completely, although I don't know of any compiler that does so.

If you want to be able to modify the contents of a string, then you need to set aside storage for that string, instead of just pointing to a string literal:

char p[] = "John"; // copies the contents of the string literal "John" to p
                   // p is sized automatically based on the length of the
                   // string literal

Here's a hypothetical memory map showing the result of that declaration and initialization:

Address            Item            0x00  0x01  0x02  0x03
-------            ----            ----  ----  ----  ----
0x8000             "John"           'J'   'o'   'h'   'n'
0x8004                             0x00  0x??   0x?? 0x??  // 0x?? represents a random byte value
...
0xfffdc100         p                'J'   'o'   'h'   'n'
0xfffdc104                         0x00  0x??  0x??  0x??

The string literal that lives at 0x8000 should not be modified; the string that lives in p at 0xfffdc100 may be modified (although the buffer will only have enough space to store up to a 4-character string).

Your second mistake (and the one causing the compiler to complain) is in this line:

*(p+2)="v";

The expression "v" is a string literal and has type "2-element array of char"; since it's not the operand of the sizeof or unary & operators, the type "decays" to "pointer to char", and the value of the expression is the address of the string literal "v".

The expression *(p + 2) has type char (it resolves to a single character value), which is an integral type, not a pointer type. You cannot assign pointer values to non-pointer objects.

You can easily fix that by changing that line to

*(p + 2) = 'v'; // note single quote vs double quote

This time, instead of trying to assign the address of a string literal to p[2], you're assigning the value of a single character.

Upvotes: 0

Sourav Ghosh
Sourav Ghosh

Reputation: 134376

Firstly, In your code,

 char *p="John";

p points to a string literal, and attempt to modify a string literal invokes undefined behavior.

Related, C11, chapter §6.4.5

[...] If the program attempts to modify such an array, the behavior is undefined.

If you want to modify , you need an array, like

char p[]="John";

Secondly, *(p+2)="v"; is wrong, as "" denotes a string, whereas you need a char (Hint: check the type os *(p+2)). Change that to

*(p+2)='v';

To elaborate the difference, quoting C11, chapter §6.4.4.4, for Character constants

An integer character constant is a sequence of one or more multibyte characters enclosed in single-quotes, as in 'x'.

and chapter §6.4.5, string literals

A character string literal is a sequence of zero or more multibyte characters enclosed in double-quotes, as in "xyz".

Thirdly, as per the C standards, void main() should at least be int main(void).

Upvotes: 2

Iharob Al Asimi
Iharob Al Asimi

Reputation: 53016

You are assigning a pointer to a string literal to an element of another string literal.

First, you need to change the pointer and make it an array, so any modification is legal, this is an example

char p[] = "John";

then you need to replace "v" which is a string literal consisting of two characters 'v' and '\0', to 'v' which is the ascii value for the letter v as an integer

*(p + 2) = 'v';

also, this is the third element and not the second, because the first element is p[0].

Upvotes: 1

Related Questions