C programming: dynamic keyboard allocation with pointer to pointer

Written by webscriptz on 16/04/2010 – 23:41

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

The view of tomorrow

12/09/2009 – 19:34

This is a video of apparently almost 20 years ago but look at it, it's really has happend. Google Video Tablet Newspaper

Yii framework very first findings

07/09/2009 – 22:46

Yii framework (yiiframework.com) is a very fast php5 framework. At first it seems like a jungle in the code, everything is put into motion to make the framework very fast. ...

Coding right, tips

21/08/2009 – 01:21

I stumbled upon this when searching for something about code, enjoy! site: freevbcode

Symfony php framework

27/07/2009 – 20:15

I'm attached to codeigniter, I know my way around but codeigniter as a a easy to use but now I want something more darring and so this is it, Symfony ...

Codeigniter Redirect

30/06/2009 – 20:45

redirect() Does a "header redirect" to the local URI specified. Just like other functions in this helper, this one is designed to redirect to a local URL within your site. ...

Finished my exams

23/06/2009 – 17:14

I'm done, it's finished....I'm finished :P Ow yeah I screwed up completly I think, unfortunantly. It's kind of a heavy realisation but this crappy school ('KdG') wasn't the place to ...

Agile Application with php & mySQL

28/03/2009 – 20:35

I'm working on a website system an facing this problem where and how to store my configurations in the database.  So you want to store your configuration vars in a ...

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 :