Thread: arrays with different sizes..

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    11

    arrays with different sizes..

    i want to define arrays with different sizes..

    something like this..
    Code:
    struct block{
    	int id;
    	int points[3];
    	int cells[3];
    	struct covar covariant[cells[0]][cells[1]][cells[2]];
    };
    however since cells is defined inside the same structure i cannot define it like this..
    what is the way out??

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Dynamic memory allocation would be one option. Look at the malloc() function.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    11
    can you elaborate..
    afaik malloc is used for pointers..
    i couldnt find its use to initialize arrays...

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You can simulate an array using malloc():
    Code:
    {
    int size;
    int *array;
    
    size = 5;
    
    array = malloc(sizeof(int) * size);
    // Now you can access array[0] through array[4].
    free(array);
    }
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Jun 2006
    Posts
    11
    hey thanks that worked.. but i am unable to rewind the pointer.. how do i do that?

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Rewind the pointer? I'm assuming you're doing something like:
    Code:
    {
      char *ptr = malloc(10);
    
      strcpy(ptr, "foo");
      while(*ptr)
      {
        putchar(*ptr);
        ptr++;
      }
      // Uh oh! I lost the original start of the memory allocation
    }
    If that's what you mean then there's no way to get it back. The way to handle that situation correctly is to leave a pointer pointing to the beginning and don't change it. Use a second pointer to walk around instead.
    Last edited by itsme86; 06-30-2006 at 02:36 PM. Reason: Oops...forgot to increment pointer
    If you understand what you're doing, you're not learning anything.

  7. #7
    Registered User
    Join Date
    Jun 2006
    Posts
    11
    hi.. i ve been rtying to do what u said..
    but it gives me segmentation fault as soon as i try to declare another pointer..
    am i missing something here..

  8. #8
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    I mean something like this:
    Code:
    {
      char *ptr, *ptr2;
    
      ptr = malloc(10);
    
      strcpy(ptr, "foo");
      ptr2 = ptr;
    
      while(*ptr2)
      {
        putchar(*ptr2);
        ptr2++;
      }
    
      free(ptr);
    }
    Maybe you could post the code you're actually trying and I or someone else can find the problem.
    If you understand what you're doing, you're not learning anything.

  9. #9
    Registered User
    Join Date
    Jun 2006
    Posts
    11
    yes i got what you were trying to say..
    but i just found that declaring any more pointers in my program causes seg. fault...
    here's the code...
    if i delelte any one of the colored part the seg. fault goes away ..
    otherwise however.. any pointer declaration gives an error..
    i have a 768 mb ram on my comp.. could it be a memory problem??

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include "structures.c"
    
    int main()
    {
    FILE *stream;
    
    if((stream=fopen("block2.dat","r"))!=NULL){
    	printf("Succesfully opened file\n");
    }
    else{
    	printf("Failed to open file\n");
    	exit(1);
    }
    
    char number[5];char temp[10];
    
    char num_i[5],num_j[5],num_k[5];
    int j=0;int k=0;int i=0;int l,m,z,loop_var;
    
    fscanf(stream,"%s", number);
    num_blocks=atoi(number);
    printf("%i\n",num_blocks);
    struct block arr_blocks[num_blocks];
    int no_i,no_j,no_k,total_points;
    
    for(i=0;i<num_blocks;i++){
    	fscanf(stream,"%s	%s	%s", num_i,num_j,num_k);
    	arr_blocks[i].id=i;
    	arr_blocks[i].points[0]=atoi(num_i);
    	arr_blocks[i].points[1]=atoi(num_j);
    	arr_blocks[i].points[2]=atof(num_k);
    	arr_blocks[i].cells[0]=(atoi(num_i))-1;
    	arr_blocks[i].cells[2]=(atoi(num_j))-1;
    	arr_blocks[i].cells[1]=(atoi(num_k))-1;
    }
    
    struct point *p_map,*p_map_2;
    struct covar ***p_covariant[num_blocks];
    struct contra ***p_contravariant[num_blocks];
    
    for(loop_var=0;loop_var<num_blocks;loop_var++){
    	total_points=(arr_blocks[loop_var].points[0])*
    				(arr_blocks[loop_var].points[1])*(arr_blocks[loop_var].points[2]);
    
    	//allocating memory for covar
    	p_covariant[loop_var]= 
    		(struct covar***)malloc(sizeof(struct covar**)*((arr_blocks[i].cells[0])));
    	for (i=0; i<(arr_blocks[i].cells[0]); i++)
    	{
    		p_covariant[loop_var][i] = 
    		(struct covar**)malloc(sizeof(struct covar*)*(arr_blocks[loop_var].cells[1]));
    		for (j=0; j<(arr_blocks[loop_var].cells[1]); j++)
    		{
    			p_covariant[loop_var][i][j] =
    				 (struct covar*)malloc(sizeof(struct covar)*
    					(arr_blocks[loop_var].cells[2]));
    		}
    	}
    
    	**p_covariant[loop_var]=&p_covariant[loop_var][0][0][0];
    
    	p_contravariant[loop_var] = (struct contr..........*)malloc(sizeof(struct contr..........)*
    					(arr_blocks[i].cells[0]));
    	for (i=0; i<(arr_blocks[i].cells[0]); i++)
    	{
    		p_contravariant[loop_var][i] = 
    		(struct contr..........)malloc(sizeof(struct contra*)*(arr_blocks[loop_var].cells[1]));
    		for (j=0; j<(arr_blocks[loop_var].cells[1]); j++)
    		{
    			p_contravariant[loop_var][i][j] =
    			 (struct contra*)malloc(sizeof(struct contra)
    						*(arr_blocks[loop_var].cells[2]));
    		}
    	}
    	**p_contravariant[loop_var]=&p_contravariant[loop_var][0][0][0];
    
    	//Read all the co-ordinates from the file
    	p_map=(struct point*)malloc(sizeof(struct point)*total_points);
    	printf("%u\n",p_map);
    	p_map_2=p_map;
    	printf("%u\n",p_map_2);
    	j=0;k=0;
    	for(k=0;k<3;k++){
    		for(i=0;i<total_points; i++){
    			fscanf(stream,"%s",temp);
    			(*p_map).coords[k]=atof(temp);
    			p_map++;
    		}
    	}
    }
    return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  4. Array sizes and arbitrary sized arrays
    By bennyandthejets in forum C++ Programming
    Replies: 3
    Last Post: 07-03-2003, 11:24 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM