Saturday, December 14, 2019

filename up to the extension and creating a new filename

Just dropping this here for now.

void getFilenameWithNoExtensionAndCreateANewFilename()
{
char *arg= (char *)"foobar_filename.ppm";
char output_filename[256];

char* ptrToPPMStart = NULL;
char* ptrCopyFrom = NULL;

char buf[80] = { 0 };
int i = 0;

ptrCopyFrom = arg;
ptrToPPMStart = strrchr(arg, '.');

while (ptrCopyFrom != ptrToPPMStart) 
{
// printf("copying %c\n", *ptrCopyFrom);
buf[i] = *ptrCopyFrom;
i++;
ptrCopyFrom++;

}

buf[i] = '\0';

printf("%s\n", buf);


strcpy_s(output_filename, "output_");

//from this create a new filename
strcat_s(output_filename, arg);

printf("output_filename: %s\n", output_filename);
}

Wednesday, December 11, 2019

Allocating of heap memory inside function with doubly nested pointers

just a cut/paste of template/pattern code here so i don't have to spend 10 minutes dancing with the compiler.

#include
#include
#include

#define SIZE 10

void fooAllocator(unsigned int** image)
{
*image = (unsigned int *) malloc(SIZE * sizeof(unsigned int));

if (*image != NULL) //resolves MSVC compiler error, just a little test before the real loop
{
(*image)[7] = 14;
}
for (int i = 0; i < SIZE; i++)
{
(*image)[i] = 66;
}
}

void fooWriter(unsigned int* image)
{
for (int i = 0; i < SIZE; i++)
{
image[i] = 42;
}
}

void printImage(unsigned int* image)
{
for (int i = 0; i < SIZE; i++)
{
printf("[%d]=%d\n", i, image[i]);
}
}

int main()
{
unsigned int* image = NULL;

//function that allocates memory for image
fooAllocator(&image);
printImage(image);
fooWriter(image);
printImage(image);

for (int i = 0; i < SIZE; i++)
{
image[i] = 36;
}

printImage(image);
return 1;
}