Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Starting with the first part of your question, let's see what happens when you use statement:</p> <pre><code>char *somestring = "somestring"; </code></pre> <p>Here,you declare a <code>character pointer</code> <strong>somestring</strong>, the string <code>somestring</code> is stored at some address in the memory and that address is allocated to the pointer <strong>somestring</strong> (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!!)</p> <p>Now you can refer to that string using this pointer <code>somestring</code>.</p> <p>Now see, how it is different in the following statement, which will produce errors:</p> <pre><code>int *someint = 45 </code></pre> <p>Here you are declaring an integer pointer <code>someint</code>(of type <code>int*</code>) but assigning an integer value (of type <code>int</code>) to it.Not only is there a <code>type</code> 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:</p> <pre><code> int num=45,*someint=&amp;num; </code></pre> <p>Here the <code>&amp;</code> <strong>address of</strong> operator assigns the address of the variable <code>num</code> to the pointer, and you can dereference it using the <code>*</code> operator as following:</p> <pre><code> printf("The value pointed by the pointer is ",*someint); </code></pre> <p>Note how <strong>*</strong> before <code>someint</code> carries a different meaning in the declaration statement and as an argument to <code>printf()</code>.</p> <p>Coming to the second part of your question, why we use <code>str1</code> in <code>printf()</code> instead of <code>*str1</code>, well, <code>str1</code> signifies the <strong>base address</strong> 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 <strong>*</strong> operator, <code>*str1</code> 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:</p> <pre><code>#include&lt;stdio.h&gt; int main(void) { char str1[10]="Davies"; printf("str1 is : %s \n but *str1 is : %c",str1,*str1); } </code></pre> <p><strong>Output</strong> <code>str1 is : Davies</code></p> <pre><code> but *str1 is : D </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload