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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. A simple C question

A simple C question

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestiondata-structurestutoriallearning
10 Posts 7 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.
  • B Offline
    B Offline
    b_girl
    wrote on last edited by
    #1

    A friend of mine is just learning how to code in C and I've been helping her a little bit along the way. There seems to be a problem with a chunk of her code, and I can't figure out how to fix it, it's been quite a while since I've looked at C so I'm a little 'fuzzy' on it. The following bit of code is taken right from her source code.

    int *suit[4];
    for(int p = 0; p <= 3; p++)
    {
    suit[p] = &p;
    }

    ... then inside a loop which will execute the following loop x = 0 to x < 4 times: printf(" %d", *suit[x]); The program exits fine, with no errors, but the only problem is, when it prints out at the end, each entry in the suit array is set at being 4 (so the 4 will print out 4 times), when what she want to print out is 0 1 2 3, if she changes the loop to: for(int p = 0; p <= 4; p++) { ... } the print statement shows that a 5 prints out in each case. I'm sure the problem comes from this suit[p] = &p; but I don't know how to fix it. Can anyone help us out? Thanks...

    M J N J J 5 Replies Last reply
    0
    • B b_girl

      A friend of mine is just learning how to code in C and I've been helping her a little bit along the way. There seems to be a problem with a chunk of her code, and I can't figure out how to fix it, it's been quite a while since I've looked at C so I'm a little 'fuzzy' on it. The following bit of code is taken right from her source code.

      int *suit[4];
      for(int p = 0; p <= 3; p++)
      {
      suit[p] = &p;
      }

      ... then inside a loop which will execute the following loop x = 0 to x < 4 times: printf(" %d", *suit[x]); The program exits fine, with no errors, but the only problem is, when it prints out at the end, each entry in the suit array is set at being 4 (so the 4 will print out 4 times), when what she want to print out is 0 1 2 3, if she changes the loop to: for(int p = 0; p <= 4; p++) { ... } the print statement shows that a 5 prints out in each case. I'm sure the problem comes from this suit[p] = &p; but I don't know how to fix it. Can anyone help us out? Thanks...

      M Offline
      M Offline
      markkuk
      wrote on last edited by
      #2

      There's only one variable p, and every element of the array suit is made to point to that element. What exactly are you trying to do in this code?

      B 1 Reply Last reply
      0
      • B b_girl

        A friend of mine is just learning how to code in C and I've been helping her a little bit along the way. There seems to be a problem with a chunk of her code, and I can't figure out how to fix it, it's been quite a while since I've looked at C so I'm a little 'fuzzy' on it. The following bit of code is taken right from her source code.

        int *suit[4];
        for(int p = 0; p <= 3; p++)
        {
        suit[p] = &p;
        }

        ... then inside a loop which will execute the following loop x = 0 to x < 4 times: printf(" %d", *suit[x]); The program exits fine, with no errors, but the only problem is, when it prints out at the end, each entry in the suit array is set at being 4 (so the 4 will print out 4 times), when what she want to print out is 0 1 2 3, if she changes the loop to: for(int p = 0; p <= 4; p++) { ... } the print statement shows that a 5 prints out in each case. I'm sure the problem comes from this suit[p] = &p; but I don't know how to fix it. Can anyone help us out? Thanks...

        J Offline
        J Offline
        John M Drescher
        wrote on last edited by
        #3

        Generally in a loop c programmers will do this (remove the =):

        int *suit[4];
        for(int p = 0; p < 4; p++)

        This will run through all 4 suits (0..3). John

        1 Reply Last reply
        0
        • B b_girl

          A friend of mine is just learning how to code in C and I've been helping her a little bit along the way. There seems to be a problem with a chunk of her code, and I can't figure out how to fix it, it's been quite a while since I've looked at C so I'm a little 'fuzzy' on it. The following bit of code is taken right from her source code.

          int *suit[4];
          for(int p = 0; p <= 3; p++)
          {
          suit[p] = &p;
          }

          ... then inside a loop which will execute the following loop x = 0 to x < 4 times: printf(" %d", *suit[x]); The program exits fine, with no errors, but the only problem is, when it prints out at the end, each entry in the suit array is set at being 4 (so the 4 will print out 4 times), when what she want to print out is 0 1 2 3, if she changes the loop to: for(int p = 0; p <= 4; p++) { ... } the print statement shows that a 5 prints out in each case. I'm sure the problem comes from this suit[p] = &p; but I don't know how to fix it. Can anyone help us out? Thanks...

          N Offline
          N Offline
          noahsarf
          wrote on last edited by
          #4

          essentially what you are doing is setting each of your array elements to the address of p; At the end of your loop you have an entire array of addresses to the p value. This is why you print out a bunch of 4's or 5's depending on your loop criteria. If you are just trying to set the array locations to the value of the integers, then there is no need to use address. create your array as : int suit[4]; this is an array of integers. Now inside the for loop, suit[p]=p; will now set your array entry to the integer value of p and you will receive your expected results from your printf statement.

          B 1 Reply Last reply
          0
          • M markkuk

            There's only one variable p, and every element of the array suit is made to point to that element. What exactly are you trying to do in this code?

            B Offline
            B Offline
            b_girl
            wrote on last edited by
            #5

            What we would like to have happen is to have suit[0] = 0 and suit[1] = 1, etc... but suit[] is a pointer, int *suit[4]; So how, in that for loop, do we say that we want suit[p] = p itself, and not the address of p? we also tried: *suit[p] = p; and suit[p] = p; neither of these works.

            1 Reply Last reply
            0
            • N noahsarf

              essentially what you are doing is setting each of your array elements to the address of p; At the end of your loop you have an entire array of addresses to the p value. This is why you print out a bunch of 4's or 5's depending on your loop criteria. If you are just trying to set the array locations to the value of the integers, then there is no need to use address. create your array as : int suit[4]; this is an array of integers. Now inside the for loop, suit[p]=p; will now set your array entry to the integer value of p and you will receive your expected results from your printf statement.

              B Offline
              B Offline
              b_girl
              wrote on last edited by
              #6

              noahsarf wrote: Now inside the for loop, suit[p]=p; will now set your array entry to the integer value of p doing it that way gives the error: '=' cannot convert from int to int* that was one of the original ways we tried it.

              R 1 Reply Last reply
              0
              • B b_girl

                A friend of mine is just learning how to code in C and I've been helping her a little bit along the way. There seems to be a problem with a chunk of her code, and I can't figure out how to fix it, it's been quite a while since I've looked at C so I'm a little 'fuzzy' on it. The following bit of code is taken right from her source code.

                int *suit[4];
                for(int p = 0; p <= 3; p++)
                {
                suit[p] = &p;
                }

                ... then inside a loop which will execute the following loop x = 0 to x < 4 times: printf(" %d", *suit[x]); The program exits fine, with no errors, but the only problem is, when it prints out at the end, each entry in the suit array is set at being 4 (so the 4 will print out 4 times), when what she want to print out is 0 1 2 3, if she changes the loop to: for(int p = 0; p <= 4; p++) { ... } the print statement shows that a 5 prints out in each case. I'm sure the problem comes from this suit[p] = &p; but I don't know how to fix it. Can anyone help us out? Thanks...

                J Offline
                J Offline
                Jesse Evans
                wrote on last edited by
                #7

                As noahsarf said, you need to set each member of suit[] to the value of p, thus: suit[p]=p; the reason why this did not seem to work for you is that you need to also change your print statement; instead of printf(" %d", *suit[x]); you need to say printf(" %d", suit[x]); Good Luck!! 'til next we type... HAVE FUN!! -- Jesse

                B 1 Reply Last reply
                0
                • B b_girl

                  noahsarf wrote: Now inside the for loop, suit[p]=p; will now set your array entry to the integer value of p doing it that way gives the error: '=' cannot convert from int to int* that was one of the original ways we tried it.

                  R Offline
                  R Offline
                  Rick York
                  wrote on last edited by
                  #8

                  Remember to declare suit as : int suit[4]; The Ten Commandments For C Programmers

                  1 Reply Last reply
                  0
                  • J Jesse Evans

                    As noahsarf said, you need to set each member of suit[] to the value of p, thus: suit[p]=p; the reason why this did not seem to work for you is that you need to also change your print statement; instead of printf(" %d", *suit[x]); you need to say printf(" %d", suit[x]); Good Luck!! 'til next we type... HAVE FUN!! -- Jesse

                    B Offline
                    B Offline
                    b_girl
                    wrote on last edited by
                    #9

                    Thank you so much! As it turns out, she has no need to use pointers anyway, so we've gotten rid of all the pointers that she's been using.

                    1 Reply Last reply
                    0
                    • B b_girl

                      A friend of mine is just learning how to code in C and I've been helping her a little bit along the way. There seems to be a problem with a chunk of her code, and I can't figure out how to fix it, it's been quite a while since I've looked at C so I'm a little 'fuzzy' on it. The following bit of code is taken right from her source code.

                      int *suit[4];
                      for(int p = 0; p <= 3; p++)
                      {
                      suit[p] = &p;
                      }

                      ... then inside a loop which will execute the following loop x = 0 to x < 4 times: printf(" %d", *suit[x]); The program exits fine, with no errors, but the only problem is, when it prints out at the end, each entry in the suit array is set at being 4 (so the 4 will print out 4 times), when what she want to print out is 0 1 2 3, if she changes the loop to: for(int p = 0; p <= 4; p++) { ... } the print statement shows that a 5 prints out in each case. I'm sure the problem comes from this suit[p] = &p; but I don't know how to fix it. Can anyone help us out? Thanks...

                      J Offline
                      J Offline
                      John R Shaw
                      wrote on last edited by
                      #10

                      This is C++:

                      int *suit[4];
                      for(int p = 0; p <= 3; p++)
                      {
                      suit[p] = &p;
                      }

                      This is C:

                      int *suit[4];
                      int p;
                      for(p = 0; p <= 3; p++)
                      {
                      suit[p] = &p;
                      }

                      This is what I think your are trying to do since what you are doing makes no since:

                      int suit[4];
                      int p;
                      for(p = 0; p <= 3; p++)
                      {
                      suit[p] = p;
                      }

                      One last note: Get out of the habit for writing p++ when not neccessary, use ++p instead. When your friend graduates from C to C++ it will eventualy save her a lot of head acks, because post increment can lead to a great deal of unexpected over head when using user define data types. INTP

                      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