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. experimenting... "to understand pointers more"

experimenting... "to understand pointers more"

Scheduled Pinned Locked Moved C / C++ / MFC
csshelpquestion
9 Posts 5 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.
  • S Offline
    S Offline
    SilverShalkin
    wrote on last edited by
    #1

    this is my code: #include using namespace std; char *pchar[6]; char character[6] = "hello"; int main() { strcpy(pchar, &character); return 0; } that is "all" my code, no more no less. the problem accures on line: strcpy(pchar, &character); im not very sure on what this'll do, what i am trying to do is, have pchar point to the address of character. i thought that if there was more than one character i should use strcpy(), but im not very sure. how would i accomplish this? "see any problems, or better way of doing things, just note it :)" Thanks! ~SilverShalkin

    P N M 3 Replies Last reply
    0
    • S SilverShalkin

      this is my code: #include using namespace std; char *pchar[6]; char character[6] = "hello"; int main() { strcpy(pchar, &character); return 0; } that is "all" my code, no more no less. the problem accures on line: strcpy(pchar, &character); im not very sure on what this'll do, what i am trying to do is, have pchar point to the address of character. i thought that if there was more than one character i should use strcpy(), but im not very sure. how would i accomplish this? "see any problems, or better way of doing things, just note it :)" Thanks! ~SilverShalkin

      P Offline
      P Offline
      Paul M Watt
      wrote on last edited by
      #2

      Two problems: 1) pchar is an array of char*, not char. So you will not be able to access the elements in the pchar array as a string. Which means that you will not be able to send that array to strcpy. What you would actually do with an array like pchar is point to 6 different strings. Then when you wanted to use one of the strings you would access the string in the array like so: strcpy(pchar[0], ...); 2) character is a char array, which is equivalent to one char*. Therefore you do not need to take the address of the character array, you could directly pass it into strcpy like this: strcpy(..., character); Good Luck


      Build a man a fire, and he will be warm for a day
      Light a man on fire, and he will be warm for the rest of his life!

      S 1 Reply Last reply
      0
      • P Paul M Watt

        Two problems: 1) pchar is an array of char*, not char. So you will not be able to access the elements in the pchar array as a string. Which means that you will not be able to send that array to strcpy. What you would actually do with an array like pchar is point to 6 different strings. Then when you wanted to use one of the strings you would access the string in the array like so: strcpy(pchar[0], ...); 2) character is a char array, which is equivalent to one char*. Therefore you do not need to take the address of the character array, you could directly pass it into strcpy like this: strcpy(..., character); Good Luck


        Build a man a fire, and he will be warm for a day
        Light a man on fire, and he will be warm for the rest of his life!

        S Offline
        S Offline
        SilverShalkin
        wrote on last edited by
        #3

        but i want to point to character, if i dake the address (&) off, it wouldnt be point to character would it? so i would have to declare my pchar like char *pchar[6]; pchar[0] = &character[0]; pchar[1] = &character[1]; pchar[2] = &character[2]; pchar[3] = &character[3]; pchar[4] = &character[4]; pchar[5] = &character[5]; ? i hope thats wrong... thats allot to type for somthing so small :( Thank! ~SilverShalkin :rose:

        R 1 Reply Last reply
        0
        • S SilverShalkin

          this is my code: #include using namespace std; char *pchar[6]; char character[6] = "hello"; int main() { strcpy(pchar, &character); return 0; } that is "all" my code, no more no less. the problem accures on line: strcpy(pchar, &character); im not very sure on what this'll do, what i am trying to do is, have pchar point to the address of character. i thought that if there was more than one character i should use strcpy(), but im not very sure. how would i accomplish this? "see any problems, or better way of doing things, just note it :)" Thanks! ~SilverShalkin

          N Offline
          N Offline
          Nish Nishant
          wrote on last edited by
          #4

          Do this instead :- char *pchar; char abc[6]; strcpy(abc,"hello"); pchar = abc; Nish


          Regards, Nish Native CPian. Born and brought up on CP. With the CP blood in him.

          S 1 Reply Last reply
          0
          • S SilverShalkin

            but i want to point to character, if i dake the address (&) off, it wouldnt be point to character would it? so i would have to declare my pchar like char *pchar[6]; pchar[0] = &character[0]; pchar[1] = &character[1]; pchar[2] = &character[2]; pchar[3] = &character[3]; pchar[4] = &character[4]; pchar[5] = &character[5]; ? i hope thats wrong... thats allot to type for somthing so small :( Thank! ~SilverShalkin :rose:

            R Offline
            R Offline
            Roger Broomfield
            wrote on last edited by
            #5

            Lets assume for a minute that you did do that. You could then do this: printf("%s\n",pchar[0]); printf("%s\n",pchar[1]); printf("%s\n",pchar[2]); printf("%s\n",pchar[3]); printf("%s\n",pchar[4]); printf("%s\n",pchar[5]); and the results would be: hello ello llo lo o <> in summary an array of characters IS a pointer to a sequence of characters. so if we were to do this instead char characters[6] = "hello"; char* pchar = characters; char* pchars = &characters[0]; then examine the values of all three variables in the debugger. they would all be identical. Hope this helps in your struggle with pointers. Roger.

            1 Reply Last reply
            0
            • N Nish Nishant

              Do this instead :- char *pchar; char abc[6]; strcpy(abc,"hello"); pchar = abc; Nish


              Regards, Nish Native CPian. Born and brought up on CP. With the CP blood in him.

              S Offline
              S Offline
              SilverShalkin
              wrote on last edited by
              #6

              would that work? - ill insert some of the code replies and try to understand this more... thanks guys! :) ~SilverShalkin :rose:

              N 1 Reply Last reply
              0
              • S SilverShalkin

                would that work? - ill insert some of the code replies and try to understand this more... thanks guys! :) ~SilverShalkin :rose:

                N Offline
                N Offline
                Nish Nishant
                wrote on last edited by
                #7

                SilverShalkin wrote: would that work? Of course, it will work :-) Nish


                Regards, Nish Native CPian. Born and brought up on CP. With the CP blood in him.

                1 Reply Last reply
                0
                • S SilverShalkin

                  this is my code: #include using namespace std; char *pchar[6]; char character[6] = "hello"; int main() { strcpy(pchar, &character); return 0; } that is "all" my code, no more no less. the problem accures on line: strcpy(pchar, &character); im not very sure on what this'll do, what i am trying to do is, have pchar point to the address of character. i thought that if there was more than one character i should use strcpy(), but im not very sure. how would i accomplish this? "see any problems, or better way of doing things, just note it :)" Thanks! ~SilverShalkin

                  M Offline
                  M Offline
                  Matt Gullett
                  wrote on last edited by
                  #8

                  WARNING: This is a long post. I think your real problem here is a misunderstanding of how C style pointers work and how to use them. Let me start by saying that a pointer is an intrinisic data type just like float, int, char, etc. The pointer data type (on Win32 systems) is a 32 bit value. This means that when you write a statement like

                  void\* pSomeValue; 
                  

                  you are telling the compiler to allocate 32 bits of memory for your program. It also identifies that you intend to populate that variable with a memory address. In the case of a void type pointer you are saying that the memory pointed to by the variable can be of any type you want. (In other words, no type affinity.) C and C++ allows pointers to have type affinity also. This means that instead of being a void pointer, you can define a pointer like

                  char\* pszSomeValue; 
                  

                  OR

                  long\* plSomeValue; 
                  

                  OR

                  CSomeClassName\* pSomeValue; 
                  

                  ETC. When you define a pointer like this you are telling the compiler that you want it to allocate 32 bits of storabe for a variable that will eventually point to a specific data type (char, long, CSomeClassName). The next subject of confusion (I think) is when you are using C style arrays of a particular data type. In your example, you used char* pchar[6] and char character[6] = "hello". The char* pchar[6] is instructing the compiler to allocate 6 memory locations each having a type affinity for a pointer to a char. This means that the compiler will allocate six 32 bit memory locations whose values it expects to be pointers to a char (char*). The char character[6] is instructing the compiler to allocate 6 memory locations each having a type affinity for a char (NOT a pointer to a char). This means that the compiler will allocate six 8 bit memory locations (that is the size of a char data type) whose value it expects to be a char. One of the nuiansces of C style arrays is that when you allocate a C style array the compiler guarantees that the memory it allocates will be continuous. This means that when you do char *pchar[6];, the compiler is going to allocate 32*6 bits in a row. When you do char character[6], the compiler is going to allocate 8*6 bits in a row. OK. That was a long winded explanation of pointers and arrays. With that understanding, lets look at the line of code where the compiler is giving you a problem.

                  strcpy(pchar, &character);  
                  

                  The strcpy function is defined as taking 2 paramaters, a char* an

                  S 1 Reply Last reply
                  0
                  • M Matt Gullett

                    WARNING: This is a long post. I think your real problem here is a misunderstanding of how C style pointers work and how to use them. Let me start by saying that a pointer is an intrinisic data type just like float, int, char, etc. The pointer data type (on Win32 systems) is a 32 bit value. This means that when you write a statement like

                    void\* pSomeValue; 
                    

                    you are telling the compiler to allocate 32 bits of memory for your program. It also identifies that you intend to populate that variable with a memory address. In the case of a void type pointer you are saying that the memory pointed to by the variable can be of any type you want. (In other words, no type affinity.) C and C++ allows pointers to have type affinity also. This means that instead of being a void pointer, you can define a pointer like

                    char\* pszSomeValue; 
                    

                    OR

                    long\* plSomeValue; 
                    

                    OR

                    CSomeClassName\* pSomeValue; 
                    

                    ETC. When you define a pointer like this you are telling the compiler that you want it to allocate 32 bits of storabe for a variable that will eventually point to a specific data type (char, long, CSomeClassName). The next subject of confusion (I think) is when you are using C style arrays of a particular data type. In your example, you used char* pchar[6] and char character[6] = "hello". The char* pchar[6] is instructing the compiler to allocate 6 memory locations each having a type affinity for a pointer to a char. This means that the compiler will allocate six 32 bit memory locations whose values it expects to be pointers to a char (char*). The char character[6] is instructing the compiler to allocate 6 memory locations each having a type affinity for a char (NOT a pointer to a char). This means that the compiler will allocate six 8 bit memory locations (that is the size of a char data type) whose value it expects to be a char. One of the nuiansces of C style arrays is that when you allocate a C style array the compiler guarantees that the memory it allocates will be continuous. This means that when you do char *pchar[6];, the compiler is going to allocate 32*6 bits in a row. When you do char character[6], the compiler is going to allocate 8*6 bits in a row. OK. That was a long winded explanation of pointers and arrays. With that understanding, lets look at the line of code where the compiler is giving you a problem.

                    strcpy(pchar, &character);  
                    

                    The strcpy function is defined as taking 2 paramaters, a char* an

                    S Offline
                    S Offline
                    SilverShalkin
                    wrote on last edited by
                    #9

                    it helps allot, thank you for spending the amount of time that it took to type that long thing :) I know how pointers and stuff work, its just, there are a couple of things that are in the gray, as in (I dont know if it will do the exact thing i want it) i guess i should use strcpy() for a pointer, "it doesnt make sense to do so :)" i think i should print this out... :) Thanks for your time! ~SilverShalkin :rose:

                    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