tomdavies
tomdavies

Reputation: 1976

A few doubts about char and int pointers in C

I have a question about pointers in C.

[1] char *somestring = "somestring"

and

[2] int *someint = 45

why [1] works and [2] not?

and why do we

printf("%s",str1);

and not

printf("%s",*str1);

?

Upvotes: 2

Views: 154

Answers (6)

Starting with the first part of your question, let's see what happens when you use statement:

char *somestring = "somestring";

Here,you declare a character pointer somestring, the string somestring is stored at some address in the memory and that address is allocated to the pointer somestring (Believe me, it's confusing to use the same name for the pointer and the contents of the string it is pointed to, as you have done!!)

Now you can refer to that string using this pointer somestring.

Now see, how it is different in the following statement, which will produce errors:

int *someint = 45

Here you are declaring an integer pointer someint(of type int*) but assigning an integer value (of type int) to it.Not only is there a type mismatch, but it can produce unpredictable behavior as you just have no idea what is already at address 45.I suppose this is what you need:

 int num=45,*someint=#

Here the & address of operator assigns the address of the variable num to the pointer, and you can dereference it using the * operator as following:

  printf("The value pointed by the pointer is ",*someint);

Note how * before someint carries a different meaning in the declaration statement and as an argument to printf().

Coming to the second part of your question, why we use str1 in printf() instead of *str1, well, str1 signifies the base address of the string ,which is nothing but the address of the first character in the array. So going by what I told you about the * operator, *str1 means the character stored at the base address of the array,ie, the first character of the array.Run the following code and it will be clear:

#include<stdio.h>

int main(void)
{   
     char str1[10]="Davies";

     printf("str1 is : %s \n but *str1 is : %c",str1,*str1);
}

Output str1 is : Davies

        but *str1 is : D

Upvotes: 1

alf
alf

Reputation: 681

int *someint = 45;

is the same as saying:

int *someint;
someint = 45;

This will compile with the warning:

initialization makes pointer from integer without a cast

And will compile without warning if you cast it: someint = (int *)45;.

This is because, you're basically saying that the "value" of someint is stored at address 45, or 0x2d, which is obviously not the case as of the declaration.

You can now add a value by dereferencing: *someint = someval;. So now someval will be stored at the address you specified earlier (though this may not work, depending on whatever the OS has stored there already).

I hope this clears things up.

Upvotes: 1

Nick
Nick

Reputation: 25828

When you declare a string literal e.g. "somestring" the compiler knows about this at compile time and puts it into a separate section of memory in your program. The compiler then assigns the memory address of this location to your string pointer.

The alternative would be to store the characters for a string on the stack:

char someString[] = { 's', 'o', 'm', 'e', ' ', 's', 't', 'r', 'i', 'n', 'g', 0 };

Because an integer is smaller than a string (and fits into a memory location) it's generally just delcared on the stack.

int someInt = 45;

Upvotes: 1

stdcall
stdcall

Reputation: 28930

[2] int *someint = 45

doesn't work because you can't cast int to int * without an explicit cast.

Edit: Actually, I just tried to compile an example code and it compiles with GCC 4.7 under linux, but it prints out an warning message.

45 is considered as an int.

[2] int *someint = (int *) 45

regarding printf, %s means, substitute %s with a char * array, and stop when you reach '\0'

Upvotes: 1

Rohan
Rohan

Reputation: 53386

In char *somestring = "somestring", "somestring" is list/array of characters.

while in int *someint = 45 45 is just one integer not list/array of ints. It is equivalent to char *somestring = 'a' which is not valid.

Upvotes: 1

John Zwinck
John Zwinck

Reputation: 249642

In C a literal string is basically a (read-only) array of characters, so char* (yes, it should be const char*, but that's lost to history). A literal int is not an array or pointer, so making a pointer to it is nonsensical. The types just don't match.

Upvotes: 5

Related Questions