Why the element and not the base address got printed?

时间:2016-02-12 21:53:35

标签: c string pointers

This is the program I wrote :

<div class="container">
  <div class="field">
    <label>
      <input class="input-checkbox" id="InputLorem" name="Inputs" type="checkbox" value="Lorem ipsum dolor sit amet" /> <span>Lorem ipsum dolor sit amet</span> </label>
  </div>

  <div class="field">
    <label>
      <input class="input-checkbox" id="InputConsectetur" name="Inputs" type="checkbox" value="Consectetur adipisicing elit" /> <span>Consectetur adipisicing elit</span> </label>
  </div>

  <div class="field">
    <label>
      <input class="input-checkbox" id="InputIpsa" name="Inputs" type="checkbox" value="Ipsa eligendi doloremque reiciendis laboriosam deleniti officia" /> <span>Ipsa eligendi doloremque reiciendis laboriosam deleniti officia</span> </label>
  </div>

  <div class="field">
    <label>
      <input class="input-checkbox" id="InputAd" name="Inputs" type="checkbox" value="Ad natus optio" /> <span>Ad natus optio </span> </label>
  </div>

  <div class="field">
    <label>
      <input class="input-checkbox" id="InputMaxime" name="Inputs" type="checkbox" value="Maxime ipsam quas culpa commodi" /> <span>Maxime ipsam quas culpa commodi</span> </label>
  </div>

  <div class="field">
    <label>
      <input class="input-checkbox" id="InputAccusamus" name="Inputs" type="checkbox" value="Accusamus quam dolorum error" /> <span>Accusamus quam dolorum error</span> </label>
  </div>

  <div class="field">
    <label>
      <input class="input-checkbox" id="InputNumquam" name="Inputs" type="checkbox" value="Numquam modi" /> <span>Numquam modi</span> </label>
  </div>
</div>

Output is

Let us C YPK 101

as required

Now what we pass to the function main() { struct book { char name[25] ; char author[25] ; int callno ; } ; struct book b1 = { "Let us C", "YPK", 101 }; display(b1.name, b1.author, b1.callno); } void display(char *s, char *t, int n) { printf ("\n%s %s %d", s, t, n); } is the base address of the strings display() and name and in the definition of the function , use pointers. So why is the function like author and not like printf("\n%s %s %d", s, t, n)?

The second one does not run . Is not printf("\n%s %s %d", *s, *t, n) the base address and s the string stored at that address?

3 个答案:

答案 0 :(得分:6)

In C, strings are simply blocks of memory that happen to have a null terminator at the end of them. The convention is to refer to strings by <!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> <head> <meta charset="utf-8" /> <title></title> </head> <body> HELLO WORLD! </body> </html> pointers that point at the address of the start of the string. As a result, the library char* function is designed so that the printf specifier expects you to provide a pointer to the start of the string that should ultimately be printed out.

If you dereference a %s pointer, you get back a single char* representing the initial element of that string. Notice that this isn't the string itself - it's just the first character. If you try to pass char to *s where a printf specifier is expected, you'll likely crash the program because %s will then jump to a memory address whose numeric value is given by the numeric value of the printf and try to start printing characters it finds there.

答案 1 :(得分:3)

The docWord.Sections[docWord.Content.Sections.Count].PageSetup.TextColumns.SetCount(3); specifier in "%s" expects a pointer to a string, a string is simply an array of bytes terminated with a special vaule printf() and '\0' is expecting the base address of such array and it also expects the printf() to be at the end, otherwise it will behave incorrectly. You are passing a pointer containing the address to the start of the memory block where the array is sotred, and the '\0' specifier will make "%s" read from the array and output the contents of it to printf().

Passing stdout to the *s is undefined behavior as it will interpret the character value "%s" as an address and try to read characters from there.

NOTE 1: s[0] should return main(), so it should be one of

int

NOTE 2: Don't put the end of line at the begining of a line, it's called end of line int main(void); int main(int argc, char *argv[]); for a reason, and that reason is that '\n' is line buffered so it requires an stdout to flush the buffer or an explicit '\n'. I don't know whether it's natural to add an fflush(stdout) at the start of a line if it's meant to end lines, in it's even called '\n' and is platform dependent.

答案 2 :(得分:-1)

C has no notion of string besides that: a pointer to a sequence of chars (to the first one). So if you do *a you are getting the first char, not a "string". All C functions that deal with so-called strings expect that kind of pointers.