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. 'The heck? How is 14==4?

'The heck? How is 14==4?

Scheduled Pinned Locked Moved C / C++ / MFC
questioncomdebuggingperformance
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.
  • L Offline
    L Offline
    Lord Kixdemp
    wrote on last edited by
    #1

    Hello everyone! Look at my function: /* getPartName: Returns the filename for a part * fileName: Original file's name * part: Part number (-1 is the .mtx file) */ char * getPartName(char * fileName, int part) { // Debug printf printf("%i\n", sizeof(fileName)); /* partName: Filename of part * numExt: Extension number (000-999, see below) * partStr: String representation of part file * unexName: Original filename without extension */ char *partName, *numExt, *partStr, *unexName; int remSpace; /* Put the unextended name on it and add space for the extension */ partName = malloc(sizeof(char) * (sizeof(getUnextendedName(fileName)) + 4)); /* Add the name to it */ strcpy(partName, getUnextendedName(fileName)); /* Do they want the main file? */ if (part == -1) strcat(partName, ".mtx"); /* Or a part file? */ else { /* Allocate memory for 4 chars (.xxx) */ partStr = malloc(sizeof(char) * 4); /* Put said chars on it */ sprintf(partStr, ".%2i", part); /* Add it */ strcat(partName, partStr); } return partName; } See the debug printf at the top? Well, why does it print out "4"? It's supposed to be 14... Even when I change that printf to display the actual filename it prints out "screenshot.png", so why the heck would it be 4? Anyone know? Thanks! Lord Kixdemp www.SulfurMidis.com www.SulfurSoft.tk [ftp://][http://][hotline://]tsfc.ath.cx

    C K A 3 Replies Last reply
    0
    • L Lord Kixdemp

      Hello everyone! Look at my function: /* getPartName: Returns the filename for a part * fileName: Original file's name * part: Part number (-1 is the .mtx file) */ char * getPartName(char * fileName, int part) { // Debug printf printf("%i\n", sizeof(fileName)); /* partName: Filename of part * numExt: Extension number (000-999, see below) * partStr: String representation of part file * unexName: Original filename without extension */ char *partName, *numExt, *partStr, *unexName; int remSpace; /* Put the unextended name on it and add space for the extension */ partName = malloc(sizeof(char) * (sizeof(getUnextendedName(fileName)) + 4)); /* Add the name to it */ strcpy(partName, getUnextendedName(fileName)); /* Do they want the main file? */ if (part == -1) strcat(partName, ".mtx"); /* Or a part file? */ else { /* Allocate memory for 4 chars (.xxx) */ partStr = malloc(sizeof(char) * 4); /* Put said chars on it */ sprintf(partStr, ".%2i", part); /* Add it */ strcat(partName, partStr); } return partName; } See the debug printf at the top? Well, why does it print out "4"? It's supposed to be 14... Even when I change that printf to display the actual filename it prints out "screenshot.png", so why the heck would it be 4? Anyone know? Thanks! Lord Kixdemp www.SulfurMidis.com www.SulfurSoft.tk [ftp://][http://][hotline://]tsfc.ath.cx

      K Offline
      K Offline
      kakan
      wrote on last edited by
      #2

      fileName is a char * And sizeof(char *) == 4. If you want to know the length, use strlen(fileName)

      1 Reply Last reply
      0
      • L Lord Kixdemp

        Hello everyone! Look at my function: /* getPartName: Returns the filename for a part * fileName: Original file's name * part: Part number (-1 is the .mtx file) */ char * getPartName(char * fileName, int part) { // Debug printf printf("%i\n", sizeof(fileName)); /* partName: Filename of part * numExt: Extension number (000-999, see below) * partStr: String representation of part file * unexName: Original filename without extension */ char *partName, *numExt, *partStr, *unexName; int remSpace; /* Put the unextended name on it and add space for the extension */ partName = malloc(sizeof(char) * (sizeof(getUnextendedName(fileName)) + 4)); /* Add the name to it */ strcpy(partName, getUnextendedName(fileName)); /* Do they want the main file? */ if (part == -1) strcat(partName, ".mtx"); /* Or a part file? */ else { /* Allocate memory for 4 chars (.xxx) */ partStr = malloc(sizeof(char) * 4); /* Put said chars on it */ sprintf(partStr, ".%2i", part); /* Add it */ strcat(partName, partStr); } return partName; } See the debug printf at the top? Well, why does it print out "4"? It's supposed to be 14... Even when I change that printf to display the actual filename it prints out "screenshot.png", so why the heck would it be 4? Anyone know? Thanks! Lord Kixdemp www.SulfurMidis.com www.SulfurSoft.tk [ftp://][http://][hotline://]tsfc.ath.cx

        C Offline
        C Offline
        Cedric Moonen
        wrote on last edited by
        #3

        filename is a pointer to a char array, and a pointer is always 4 bytes long. Use strlen instead.


        Cédric Moonen Software developer
        Charting control

        1 Reply Last reply
        0
        • L Lord Kixdemp

          Hello everyone! Look at my function: /* getPartName: Returns the filename for a part * fileName: Original file's name * part: Part number (-1 is the .mtx file) */ char * getPartName(char * fileName, int part) { // Debug printf printf("%i\n", sizeof(fileName)); /* partName: Filename of part * numExt: Extension number (000-999, see below) * partStr: String representation of part file * unexName: Original filename without extension */ char *partName, *numExt, *partStr, *unexName; int remSpace; /* Put the unextended name on it and add space for the extension */ partName = malloc(sizeof(char) * (sizeof(getUnextendedName(fileName)) + 4)); /* Add the name to it */ strcpy(partName, getUnextendedName(fileName)); /* Do they want the main file? */ if (part == -1) strcat(partName, ".mtx"); /* Or a part file? */ else { /* Allocate memory for 4 chars (.xxx) */ partStr = malloc(sizeof(char) * 4); /* Put said chars on it */ sprintf(partStr, ".%2i", part); /* Add it */ strcat(partName, partStr); } return partName; } See the debug printf at the top? Well, why does it print out "4"? It's supposed to be 14... Even when I change that printf to display the actual filename it prints out "screenshot.png", so why the heck would it be 4? Anyone know? Thanks! Lord Kixdemp www.SulfurMidis.com www.SulfurSoft.tk [ftp://][http://][hotline://]tsfc.ath.cx

          A Offline
          A Offline
          Alton Williams
          wrote on last edited by
          #4

          The reason is because your using the sizeof operator to request the size of the array an not the pointer eg:

          #include 
          void main() // returning a void is NOT recomended
          {
            char *pszSite = "Code project";
            cout << "The size of string is: " << (sizeof)pszSite << endl; // prints 12
            cout << "The size of character is: " << (sizeof)*pszSite << endl; // prints 1
            cout << "The size of pointer is: " << (sizeof)&pszSite << endl; // prints 4
          }
          

          In other words with using pointers to chars

          • No prefix operator treats it as a string.
          • Deferencing (asterisk) operator means get the contents to where it is pointing to.
          • Address of (ampersand) operator means get the actual address of the pointer

          I hope that makes sense Alton

          L 1 Reply Last reply
          0
          • A Alton Williams

            The reason is because your using the sizeof operator to request the size of the array an not the pointer eg:

            #include 
            void main() // returning a void is NOT recomended
            {
              char *pszSite = "Code project";
              cout << "The size of string is: " << (sizeof)pszSite << endl; // prints 12
              cout << "The size of character is: " << (sizeof)*pszSite << endl; // prints 1
              cout << "The size of pointer is: " << (sizeof)&pszSite << endl; // prints 4
            }
            

            In other words with using pointers to chars

            • No prefix operator treats it as a string.
            • Deferencing (asterisk) operator means get the contents to where it is pointing to.
            • Address of (ampersand) operator means get the actual address of the pointer

            I hope that makes sense Alton

            L Offline
            L Offline
            Lord Kixdemp
            wrote on last edited by
            #5

            Thanks you all! It works now! PD: That's what I get for coding at 8:30 AM before even going to sleep, and after not coding C in a while... Lord Kixdemp www.SulfurMidis.com www.SulfurSoft.tk [ftp://][http://][hotline://]tsfc.ath.cx

            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