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. Ah, pointers...

Ah, pointers...

Scheduled Pinned Locked Moved C / C++ / MFC
data-structuresquestionlounge
3 Posts 2 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
    Shankar Chandra Bose
    wrote on last edited by
    #1

    Chaps and fellow geeks, I have a pointer to an array of 10 chars (note that this is different from an array of ten chars, which allocates 10 bytes, the former allocates 4 bytes). This means, whenever I increment the pointer, I can skip by 10 bytes. I then proceed to allocate 100 bytes of storage (as I want to treat it as a 10 by 10 array). Trouble is (well, not really trouble), when I use new to allocate 100 bytes for the pointer I need to typecast the pointer returned by the new operator to the type of "pointer to an array". Towards this end, I have typedefed CHARTENARRAY as a pointer to an array of 10 bytes. I then typecast the return value of new like so: pArray = (CHARTENARRAY) new char[100]; I would like to know if there is a way of doing this *without* the typedef. How would I typecast it? In general, what would be the cast for a pointer to an array of x bytes that is returned, by the new operator when it is used to allocate x bytes? To make things clear, here is the source (with lots of comments), you can compile it and run it if you'd like to experiment: typedef char (*CHARTENARRAY)[10]; // typedefines CHARTENARRAY to be a pointer to an array of 10 b CHARTENARRAY pArray = NULL; pArray = (CHARTENARRAY) new char[100]; // need to do this WITHOUT the (CHARTENARRAY) typedef. for(int i = 0; i < 10; ++i) { strcpy(*pArray, "HELLO!"); // Fill in 10 "HELLO!" strings in 10 slots ++pArray; // increment by 10 bytes (scalar) } pArray -= 10; // Go to array start (will decrement by 100 bytes, since scalar is 10) for(i = 0; i < 10; ++i) // Display routine { puts(*pArray); ++pArray; // Go to next string } pArray -= 10; // Go to array start delete [] pArray; // Free

    M 1 Reply Last reply
    0
    • S Shankar Chandra Bose

      Chaps and fellow geeks, I have a pointer to an array of 10 chars (note that this is different from an array of ten chars, which allocates 10 bytes, the former allocates 4 bytes). This means, whenever I increment the pointer, I can skip by 10 bytes. I then proceed to allocate 100 bytes of storage (as I want to treat it as a 10 by 10 array). Trouble is (well, not really trouble), when I use new to allocate 100 bytes for the pointer I need to typecast the pointer returned by the new operator to the type of "pointer to an array". Towards this end, I have typedefed CHARTENARRAY as a pointer to an array of 10 bytes. I then typecast the return value of new like so: pArray = (CHARTENARRAY) new char[100]; I would like to know if there is a way of doing this *without* the typedef. How would I typecast it? In general, what would be the cast for a pointer to an array of x bytes that is returned, by the new operator when it is used to allocate x bytes? To make things clear, here is the source (with lots of comments), you can compile it and run it if you'd like to experiment: typedef char (*CHARTENARRAY)[10]; // typedefines CHARTENARRAY to be a pointer to an array of 10 b CHARTENARRAY pArray = NULL; pArray = (CHARTENARRAY) new char[100]; // need to do this WITHOUT the (CHARTENARRAY) typedef. for(int i = 0; i < 10; ++i) { strcpy(*pArray, "HELLO!"); // Fill in 10 "HELLO!" strings in 10 slots ++pArray; // increment by 10 bytes (scalar) } pArray -= 10; // Go to array start (will decrement by 100 bytes, since scalar is 10) for(i = 0; i < 10; ++i) // Display routine { puts(*pArray); ++pArray; // Go to next string } pArray -= 10; // Go to array start delete [] pArray; // Free

      M Offline
      M Offline
      Michael Dunn
      wrote on last edited by
      #2

      Don't try to do it without the typedef. Using the typedef makes the code easier to understand. Well, ok, to answer, I think the cast would be (char (*)[10]). But honestly, only Bjarne Stroustroup would know what that means. ;) Stick with the typedef. --Mike-- http://home.inreach.com/mdunn/ #include "buffy_sig"

      S 1 Reply Last reply
      0
      • M Michael Dunn

        Don't try to do it without the typedef. Using the typedef makes the code easier to understand. Well, ok, to answer, I think the cast would be (char (*)[10]). But honestly, only Bjarne Stroustroup would know what that means. ;) Stick with the typedef. --Mike-- http://home.inreach.com/mdunn/ #include "buffy_sig"

        S Offline
        S Offline
        Shankar Chandra Bose
        wrote on last edited by
        #3

        Won't forget that in a hurry-it works, thanx Mike. I did try (char*)[10] - this didn't work, though I thought that was intuitive :) Looks like the right to left rule of reading declarations might come in handy even in areas such as this! Cheers, Shanker.

        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