Archive for April, 2010

C programming: dynamic keyboard allocation with pointer to pointer

Friday, April 16th, 2010

Something I’ve written for a project I have to do for school, it’s without bugs at least on visual studio 2008.

The little story about this code:

This is a dynamic memory allocation function for text entered by the user.

You give the addresses from pointers to memory placements to the function, it only return errors by value, everything else is returned by reference (pointer to pointer).

**string is the string pointer
**cntrChar is a counter for the number for characters

**cntrChar is used in another function to verify that the chain of characters entered, is longer then 1 because ‘\n’ is also seen as character so you need more then 1 to pass that test normally but it depends on the requirements of the program created.


int keyboardInputString(char **string, int **cntrChar){

	char c, *iString;

	int i=0;

	iString = (char*)malloc(sizeof(char));

	if(iString != NULL){

		do{

			c = getchar();

			*(iString+i)=c;

			i++;

			iString = (char*) realloc (iString, (i+1) * sizeof(char));

			if(iString == NULL)

				return 1;

		}while(c != '\n');

		*(iString+i-1)='\0';

		*string = iString;

		*cntrChar = &i;

		return 0;

	}

	else{

		return 1;

	}

}

exemple:

   if(strcmp(**string, "exit") && **cntrChar >1)
     //your code here

Windows ate my thumbdrive

Friday, April 2nd, 2010

Yesterday was disappointing and frustrating.

Visual studio freaked out which resulted in my project becoming inaccessible from my thumb drive and not long after that the partition table was destroyed, resulting in my thumb drive being of the RAW format.

I searched and found some recovery software but nothing that wanted to recover my partition table so I had to result to formatting and recovering data afterwords. Unfortunately I got to save every file that was on the drive except for my project which visual studio had obviously eaten up.

Thanks again Microsoft, nice tool, if only it wasn’t this hungry! Come to think of it almost everyone had problems with visual studio, maybe it wanted revenge for the bad code that we write. :P

About Me

Here I'll share my knowledge, discovery and experience related to my hobby and work. Most articles on this site are related to blog design, short reviews, tips and make money online. More

Find entries :