Thread: Pointer question

  1. #16
    Registered User
    Join Date
    Jan 2009
    Posts
    22
    i have an idea for my problem:

    if i use malloc in a loop so that it allocates memory in increments for an array, will it affect the elements of the array stored before?

    for example:

    Code:
    int *matrix;
    
    for (x=1, matrix[y]!='/n', x++)
    {
    matrix = (int *)malloc(x * sizeof(int));
    matrix[y]=fgets(....);
    y++;
    }
    will this work? or is this a stupid idea?

    please help.

  2. #17
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, it won't work the way you have written it, because for every x, you allocate another block of memory, loosing all the old information.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #18
    Registered User
    Join Date
    Jan 2009
    Posts
    22

    Question

    thanks, i've realized that.

    is it really possible to store a string using fgets without first knowing the size of the string to be entered?

    i mean what do i put in the "size" in fgets(string, size, stdin); ?

    how do i use pointers to remedy this?


    please help. i'm really stuck on this input problem! thanks!

  4. #19
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You COULD write a function that reads ANY length of line, but I would probably approach it with a suitably enormous buffer as that means you can use fgets(). You can validate that the buffer is big enough.

    It seems unlikely that a line would have more than 1000 floating point numbers in them, so if we choose a 32000 char array to read the line into, we can have at least 1000 numbers in the line (we do not RESTRICT it to 1000 numbers in itself, but 1000 numbers with lots of digits is a reasonable assumption. If it's enough to hold 2000 15-digit numbers or 4000 7- digit numbers, then that's allowed too, if you see what I mean).

    Then just use a local array of 32000, and call fgets(array, sizeof(array), ...) to read the line.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #20
    Registered User
    Join Date
    Jan 2009
    Posts
    22
    i do think that would work. but the problem is the theoretical question of what if it's more than that? it's not just i need the program to work, but also i need the code to be correct.


    is the answer to my problem realloc()?
    i'm just stuck on how i should write this input part with the array in a pointer
    by using fgets with malloc() and realloc() .


    please help. i'm really, really confused. thanks!

  6. #21
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you must deal with lines that are "infinitely long", then you will need to use some sort of dynamic allocation to read the line in. I find it HIGHLY unlikely that you will have a line longer than 32000 bytes - the file would be 32MB if it's got 31 digits per number, 64 MB for 15 digits per number, 128MB for 7 digits per number, etc.

    But yes, you would have to loop around using realloc whenever you find yourself out of a buffer. I would start with a malloc of 1000 bytes, and then double as needed to avoid lots of copying.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #22
    Registered User
    Join Date
    Jan 2009
    Posts
    22
    problems with the multiplication of the matrices. help! please. thanks!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int inputexponent(){
    
    	//INPUTS THE EXPONENT
    
    	char exp[100]; 
    //	fgets(exp, sizeof(exp), stdin);
    	int exponent1;
    //	exponent1 = atoi(exp);
    
    	int extra;
    	while(exp[extra] != '\n'){
    		exp[extra] = getchar();
    		extra = extra + 1;
    		if(exp[extra] = ' '){
    			return 0;
    		}
    	}
    
    	return exponent1;
    } 
    
    int countelements(char inputmatrix2[]){
    
    	//COUNT NUMBER OF ELEMENTS
    
    	int x = 0;
    	int counter = 1;	
    	while(inputmatrix2[x] != '\n'){
    		if (inputmatrix2[x] != ' '){
    			switch(inputmatrix2[x-1]){
    				case '0':
    				case '1':
    				case '2':
    				case '3':
    				case '4':
    				case '5':
    				case '6':
    				case '7':
    				case '8':
    				case '9':
    				case '.': break;
    				case ' ': counter = counter + 1; break;
    			}
    		 
    		}
    		x = x + 1;
    	}
    	
    	return counter; 
    }
    
    int inputmatrixpart2(double **matrix2, int cols2, int numberofelements1){
    
    	//INPUT 2ND TO LAST ROW OF MATRIX
    
    	char inputmatrix2[500]; 
    	int e = 1;
    	int f = 1;
    	int g = 0; 
    	int numelements = 0;
    	int checkelements; 
    	char separator1[] = " ";
    	char *result1 = NULL;
    
    	while(e !=  numberofelements1){	
    		fgets(inputmatrix2, sizeof(inputmatrix2), stdin);
    
    		numelements = countelements(inputmatrix2);		//CHECKS IF MATRIX IS SQUARE
    			if(numelements !=  cols2){
    				return 0;
    			}
      
    		checkelements = elementchecker(inputmatrix2); 		//CHECKS IF INPUT IS VALID(NUMBERS ONLY)
    			if(checkelements == 0){
    				return 0;
    			}
    
    		result1 = strtok(inputmatrix2, separator1);
    		
    		while(result1 != NULL){
    			matrix2[f][g] = atof(result1);
    			g = g + 1;
    			result1 = strtok(NULL, separator1);
    		}
    		g = 0;
    		f = f + 1;
    		e = e + 1;
    	}
    	return 1;
    }
    
    int elementchecker(char inputmatrix3[]){
    	int g = 0;
    	while(inputmatrix3[g] != '\n'){
    		if((inputmatrix3[g] >= 'a')&&(inputmatrix3[g] <= 'z')){
    			return 0;
    		}else if ((inputmatrix3[g] >= 'A')&&(inputmatrix3[g] <= 'Z')){
    			return 0;
    		}
    	g = g + 1;
    	}
    	return 1;
    }
    
    void printmatrix(double **matrix1, int cols1, int rows1){
    
    	// PRINTS THE WHOLE MATRIX
    
    	printf("= \n");
    
    	int c = 0;		
    	int d = 0;
    	while(c < rows1){
    		while(d < cols1){
    			printf("%0.2lf ", matrix1[c][d]);
    			d = d + 1;
    		}
    		printf("\n");
    		d = 0;
    		c = c + 1;
    	}
    
    }
    
    
    int main(){
    	
    	//GETS THE FIRST ROW OF ELEMENTS
    	char inputmatrix[500];
    	fgets(inputmatrix, sizeof(inputmatrix), stdin);
    	
    	int numberofelements;
    	numberofelements = countelements(inputmatrix); //COUNTS THE NUMBER OF ELEMENTS IN THE FIRST ROW
    // -----------------------------------------------------------------------------------
    	//INITIALIZE INPUT MATRIX
    	int cols = numberofelements;
    	int rows = numberofelements; 
    	
    	double **matrix;						
    	matrix = (double **)malloc(rows * sizeof(double*));
    
    	int a = 0;
    	for(; a < rows; a++){
    		matrix[a] = (double *)malloc(cols * sizeof(double));
    	}
    
    	//INITIALIZE EXTRA MATRIX
    	double **extra;						
    	extra = (double **)malloc(rows * sizeof(double*));
    
    	int m = 0;
    	for(; m < rows; m++){
    		extra[m] = (double *)malloc(cols * sizeof(double));
    	}
    
    	//INITIALIZE PRODUCT MATRIX
    	double **product;						
    	product = (double **)malloc(rows * sizeof(double*));
    
    	int n = 0;
    	for(; n < rows; n++){
    		product[n] = (double *)malloc(cols * sizeof(double));
    	}
    // -----------------------------------------------------------------------------------
    	//INPUT ELEMENTS INTO MATRIX (1ST ROW ONLY)
    
    	char separator[] = " ";
    	char *result = NULL;
    	result = strtok(inputmatrix, separator);
    
    	int b = 0;
    	while(result != NULL ) {
    		matrix[0][b] = atof(result);
    		b = b + 1;	
    		result = strtok(NULL, separator);
    	}
    // -----------------------------------------------------------------------------------
    	//INPUT ELEMENTS INTO MATRIX (2ND TO LAST ROW)
    
    	int terminator;
    	terminator = inputmatrixpart2(matrix, cols, numberofelements); 
    		if(terminator == 0){						// CHECKS IF THE INPUT IS A SQUARE MATRIX, IF NOT, TERMINATE
    			return 0;
    		}
    // -----------------------------------------------------------------------------------
    	int exponent;
    	exponent = inputexponent();    //GET EXPONENT
    	if(exponent == 0){
    		return 0;
    	}
    
    	//EXTRA MATRIX GETS(COPIES) CONTENTS OF INPUT MATRIX
    	int o = 0;
    	int p = 0;
    	for(;o < rows; o++){
    		for(;p < cols; p++){
    			extra[o][p] = matrix[o][p];
    		}
    		p = 0;
    	}
    // -----------------------------------------------------------------------------------
    	//EXPONENTIATION PROCESS (ERROR!!!)
    
    	int z = 0;
    	int q = 0;				
    	int r = 0;
    	int s = 0;
    	int t = 0;
    	int w = 0;
    	int x = 0;
    	
    	while(z <= exponent){
    		for(;q < rows; q++){
    			while(t < cols){
    				for(;r < cols; r++){
    					product[q][r] = ((product[q][r]) + ((matrix[q][s]) * (extra[t][r])));
    				} 
    				r = 0;
    				s = s + 1;
    				t = t + 1;	
    			}
    			t = 0;
    			s = 0;
    		}
    		z = z + 1;
    		q = 0; 
    
    		w = 0;
    		x = 0;
    		for(;w < rows; w++){
    			for(;x < cols; x++){
    				extra[w][x] = product[w][x];
    			}
    			x = 0;
    		}
    	}
    
    	printmatrix(product, cols, rows);  //PRINT PRODUCT MATRIX
    // -----------------------------------------------------------------------------------
    	//FREE THE MEMORY ALLOCATION
    	int h;
    	for(h = rows-1; h >= 0; h--){
    		free(matrix[h]);
    	}
    		free(matrix);
    	
    	int i;
    	for(i = rows-1; i >= 0; i--){
    		free(extra[i]);
    	}
    		free(extra);
    
    	int j;
    	for(j = rows-1; j >= 0; j--){
    		free(product[j]);
    	}
    		free(product);
    // -----------------------------------------------------------------------------------
    	printf("\n");
    	return 0; // END
    	
    }

  8. #23
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    	int x = 0;
    	int counter = 1;	
    	while(inputmatrix2[x] != '\n'){
    		if (inputmatrix2[x] != ' '){
    			switch(inputmatrix2[x-1]){
    				case '0':
    If the string starts with a non-space [which I think is likely], it will access outside of the array.

    Code:
    	//GETS THE FIRST ROW OF ELEMENTS
    	char inputmatrix[500];
    	fgets(inputmatrix, sizeof(inputmatrix), stdin);
    	
    	int numberofelements;
    	numberofelements = countelements(inputmatrix); //COUNTS THE NUMBER OF ELEMENTS IN THE FIRST ROW
    Declaring variables AFTER calling functions is C99 or C++. What standard are you supposed to follow?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Easy pointer question
    By Edo in forum C++ Programming
    Replies: 3
    Last Post: 01-19-2009, 10:54 AM
  3. char pointer to pointer question
    By Salt Shaker in forum C Programming
    Replies: 3
    Last Post: 01-10-2009, 11:59 AM
  4. Pointer question
    By rakan in forum C++ Programming
    Replies: 2
    Last Post: 11-19-2006, 02:23 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM