Updates from April, 2010 Toggle Comment Threads | Keyboard Shortcuts

  • webscriptz 23:41 on 16/04/2010 Permalink | Reply
    Tags: allocation, by reference, c, c programming, char, double pointers, malloc, pointer to pointer, realloc   

    C programming: dynamic keyboard allocation with pointer to pointer 

    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
    

     
  • webscriptz 09:00 on 02/04/2010 Permalink | Reply  

    Windows ate my thumbdrive 

    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

     
c
compose new post
j
next post/next comment
k
previous post/previous comment
r
reply
e
edit
o
show/hide comments
t
go to top
l
go to login
h
show/hide help
shift + esc
cancel