Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Code Segment Question

Code Segment Question

Scheduled Pinned Locked Moved C / C++ / MFC
question
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Loli10
    wrote on last edited by
    #1

    Consider the following section of code: int a = 10; int* b; Which of the following statements places the address of a in b. Remember, more than one of these options may be correct. In your response, explain which ones work and why, and also explain why the incorrect answers will not work. I want more than a listing of the syntax errors! b = a; b = &a; b = *a; *b = a; &b = &a; Loli10

    T M 2 Replies Last reply
    0
    • L Loli10

      Consider the following section of code: int a = 10; int* b; Which of the following statements places the address of a in b. Remember, more than one of these options may be correct. In your response, explain which ones work and why, and also explain why the incorrect answers will not work. I want more than a listing of the syntax errors! b = a; b = &a; b = *a; *b = a; &b = &a; Loli10

      T Offline
      T Offline
      Tomasz Sowinski
      wrote on last edited by
      #2

      Is it your homework? Tomasz Sowinski -- http://www.shooltz.com

      L 1 Reply Last reply
      0
      • T Tomasz Sowinski

        Is it your homework? Tomasz Sowinski -- http://www.shooltz.com

        L Offline
        L Offline
        Loli10
        wrote on last edited by
        #3

        Not quite. It's something that came up in lecture. My homework is writing a program. Loli10

        1 Reply Last reply
        0
        • L Loli10

          Consider the following section of code: int a = 10; int* b; Which of the following statements places the address of a in b. Remember, more than one of these options may be correct. In your response, explain which ones work and why, and also explain why the incorrect answers will not work. I want more than a listing of the syntax errors! b = a; b = &a; b = *a; *b = a; &b = &a; Loli10

          M Offline
          M Offline
          MJ_Karas
          wrote on last edited by
          #4

          Loli10: b=a; will give compile time error becasue it is not legal to assign the int value from a to the pointer value in b. b=&a; this moves the address of a to the pointer variable b....the answer you were looking for. b=*a; this is not legal because *a means get contents pointed to by a and yet a is not a pointer variable. *b = a; this is a legal statement IF b has a valid pointer in it. If so then the value of a is moved to the integer pointed to by b. &b=&a; this is not legal becasue it does not make logical sense to use the & address of operator on the left side of an = sign. Mike

          L 1 Reply Last reply
          0
          • M MJ_Karas

            Loli10: b=a; will give compile time error becasue it is not legal to assign the int value from a to the pointer value in b. b=&a; this moves the address of a to the pointer variable b....the answer you were looking for. b=*a; this is not legal because *a means get contents pointed to by a and yet a is not a pointer variable. *b = a; this is a legal statement IF b has a valid pointer in it. If so then the value of a is moved to the integer pointed to by b. &b=&a; this is not legal becasue it does not make logical sense to use the & address of operator on the left side of an = sign. Mike

            L Offline
            L Offline
            Loli10
            wrote on last edited by
            #5

            Thanks Mike - it makes sense! Loli10

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups