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. returning arrays?

returning arrays?

Scheduled Pinned Locked Moved C / C++ / MFC
data-structuresquestion
5 Posts 4 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.
  • M Offline
    M Offline
    marouane miftah el kheir
    wrote on last edited by
    #1

    can some one tell me please how do you write the return type of a function that would return an array of characters? thanks in advance!

    M 1 Reply Last reply
    0
    • M marouane miftah el kheir

      can some one tell me please how do you write the return type of a function that would return an array of characters? thanks in advance!

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

      I am assuming you want a standard "c" style array. In that case, your function prototype should look like this: char* SomeFunction();

      N 1 Reply Last reply
      0
      • M Matt Gullett

        I am assuming you want a standard "c" style array. In that case, your function prototype should look like this: char* SomeFunction();

        N Offline
        N Offline
        Nemanja Trifunovic
        wrote on last edited by
        #3

        Matt Gullett wrote: char* SomeFunction(); :suss: I'm not so sure. How would he know the number of elements? Unless the array is zero-terminated, but then he would call it a string and not an array of characters. I vote pro drink :beer:

        M 1 Reply Last reply
        0
        • N Nemanja Trifunovic

          Matt Gullett wrote: char* SomeFunction(); :suss: I'm not so sure. How would he know the number of elements? Unless the array is zero-terminated, but then he would call it a string and not an array of characters. I vote pro drink :beer:

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

          You are correct, he asked for an "array of characters" and I assumed he wanted a string. To return an array with no predefined terminators or size, you could do one of several things. 1. Create a custom class to deal with it. 2. Instead of using a return value, use function parameters void SomeFunction(char** pszValue, int& iLen); This would be good if the function needed to determine the length and allocate the memory. OR void SomeFunction(char* pszValue, int iMaxLen); This would be good if the function just needed a pre-exising array of max size. 3. Use a byte array and use the first N bytes to specify the length. I am sure there are many other ways to do this also. Matt Gullett

          A 1 Reply Last reply
          0
          • M Matt Gullett

            You are correct, he asked for an "array of characters" and I assumed he wanted a string. To return an array with no predefined terminators or size, you could do one of several things. 1. Create a custom class to deal with it. 2. Instead of using a return value, use function parameters void SomeFunction(char** pszValue, int& iLen); This would be good if the function needed to determine the length and allocate the memory. OR void SomeFunction(char* pszValue, int iMaxLen); This would be good if the function just needed a pre-exising array of max size. 3. Use a byte array and use the first N bytes to specify the length. I am sure there are many other ways to do this also. Matt Gullett

            A Offline
            A Offline
            alex barylski
            wrote on last edited by
            #5

            I guess technically speaking there is a difference. PASCAL strings i think have a runlength encoding byte at index zero. I've always considered them to be synonymous while using Visuall C++. your bad is my bad brutha!:) "An expert is someone who has made all the mistakes in thier field" - Niels Bohr

            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