0

I have the following code:

int main(int argc, char **argv)
{
printf("%s\n",*argv);
int test = 5;
char* p;
*pint = test;
p = "banana";
printf("%s\n",p);
printf("%d\n",*pint);
}

Why is it that I have to write p="banana" and not *p="banana" but for an integer, it needs to be *pint, otherwise it will only print the address of the integer? Shouldn't p print the address of "banana" ?

Alexandru Antochi
  • 1,295
  • 3
  • 18
  • 42
  • What do expect to be stored in `p` when you say `p="banana"`? – cdarke Oct 25 '16 at 06:50
  • [Please see this discussion on why not to cast the return value of `malloc()` and family in `C`.](http://stackoverflow.com/q/605845/2173917). – Sourav Ghosh Oct 25 '16 at 06:55
  • 1
    `char* p = (char*)malloc(sizeof(char));` and then `p = "banana";`....indication of the need to clear up the basics.... :) – Sourav Ghosh Oct 25 '16 at 06:56
  • 1
    You don't "have to" do that. You chose to do that. `*x =` means to update the value stored in the space pointed to by `x`. `x =` means to make `x` point to something else. `p = "banana"` leaks memory and makes `p` point to where `"banana"` already is. It would be perfectly fine to instead write into the space pointed to by `p`: `*p = 'b';` – M.M Oct 25 '16 at 06:56
  • I revised my code as it had some lines from other tries. – Alexandru Antochi Oct 25 '16 at 06:59
  • 1
    The `%s` format specifier requires a *pointer* argument. The `%d` format specifier requires a *value* argument. Nothing to do with constants. – Weather Vane Oct 25 '16 at 07:06
  • 3
    As it stands the code won't compile, as `pint` is not defined. – alk Oct 25 '16 at 07:17
  • Also misses at least one `#include`. Please post code that is compilable after a single copy-&-paste-into-editor step. – Christian Hackl Oct 25 '16 at 09:57

3 Answers3

0
  1. You are comparing array and integer variable behavior !

  2. p = "banana";

You are assigning base address of string "banana" to pointer p.

And the printf function prototype is

int printf( const char *restrict format, ... );

printf("%s\n",p);

Above statement implies that you are passing pointer p as a argument to a function printf which holds address of string "banana"

Vijay S B
  • 1,251
  • 1
  • 13
  • 24
  • ... with the "*base address*" of a C-string being the address of the C-string's first `char`. – alk Oct 25 '16 at 08:35
-1

You are printing with %s. This prints a C string taking an input as the address of the first byte of the string.

If you print it with %p, you will get the address.

printf("%p\n",p);
Rishikesh Raje
  • 8,556
  • 2
  • 16
  • 31
-1

A C-style string is an array of characters terminated by '\0'. So when you assign a string to it, this is what it looks like;

char p[] = {'b', 'a', 'n', 'a', 'n', 'a', '\0'};

So when say, you print out p using format specifier %s, it will continue to print the remaining characters till it reaches the null terminating character.

Doing this printf("%c", *p) will only print the first character.

Using an integer, if you do this;

int p[] = {1,2,3,4,5};

And print it out;

print("%d", *p);

You only get the first integer in the array.

Note; each format specifier has its kind of value it accepts. That is why they're called format specifiers

PS:

I've edited my answer based on user2079303 and alk's comments!

  • 1
    This answer it mostly correct, but has a few misleading problems in the details: `char*` doesn't necessarily point to a null terminated string, even though that's how it's most commonly used. Your use of `p` as an array is a bit confusing since it conflicts with OP's use as a pointer. Integer array *can* technically be null terminated as well. However, not being able to use 0 as a valid value is a serious drawback, so that isn't very common. – eerorika Oct 25 '16 at 07:54
  • 1
    "*A C-style string `char *` ...*" a C-string isn't a `char*`, it isn't a pointer to a `char`. A C-string is an array of `char` with at least one element of the array being equal to `'\0'`. – alk Oct 25 '16 at 08:37
  • I used `p` as an array to show him how `char *p` handles `"banana"` internally. You can terminate an integer array with the null terminating character but if you print out all the elements of the array, it will print out the null terminating character which which will produce unexpected results and of no use in an integer array. But with an array of `char`, it serves as the end of printing out the elements and is useful. I understand your point. – Richardson Ansong Oct 25 '16 at 08:39
  • Sure, I understand your intention. But still, this "*`char *` is an array of characters*" is just plain wrong and therefore should *not* be part of an otherwise *correct* answer. – alk Oct 25 '16 at 08:41