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