NO

Author Topic: defining and declaring an array of variable size problems  (Read 6573 times)

EdPellesC99

  • Guest
defining and declaring an array of variable size problems
« on: July 16, 2010, 05:27:40 AM »

I I need to define/declare an integer array based on the number of lines I read in from a text file.

e.g. My program determines there are 54 lines in a user generated text file. The number placed in variable "line_count"


e.g. to define/declare an array called "edsarray".

static int edsarray[54] is what I need (with null cap), but of course:

static int edsarray[line_count] will not work because the arraysize must be a constant.

I can make the array supersized for all likely possibilities, and that would work for me, but is there a better way?

   Am I missing something?

Thanks Ed


Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: defining and declaring an array of variable size problems
« Reply #1 on: July 16, 2010, 08:06:56 AM »
Time to learn malloc() realloc() free() functions.
May the source be with you

EdPellesC99

  • Guest
Re: defining and declaring an array of variable size problems
« Reply #2 on: July 16, 2010, 04:40:15 PM »

Timo,



    OK, here I go! .......... may take a bit :)

 thanks, Ed

EdPellesC99

  • Guest
Re: defining and declaring an array of variable size problems
« Reply #3 on: July 17, 2010, 03:19:23 AM »


   Ok,

   Working out the bugs in this two file program, let me solve my original problem.
Thanks again Timo for steering me in the right direction.

........ Ed

Source file with main first, then include source file.





// // ~ •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •
Quote


"Calloc used to declare array in aux file function GOLD.c"

#include <stdio.h>
#include <stdlib.h>
#include "make containarray.c"

int line_num = 10;

// // ~ Ednt: I am able to size the array "containarray" with a variable, not a constant (dynamically).

int main(int argc, char* argv[])
{
   LetsMakeContainarray(line_num);
return 0;
}
// // ~ •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •

   And now aux file "make containarray.c":


int n;
int * containarray;
int extern line_num;

void LetsMakeContainarray(int);
   
void LetsMakeContainarray(line_num1)
{
   containarray = (int*) calloc (line_num1, sizeof(int));
   printf("You are going to create an integer array of %i cells.\n\nHit any Key to do so.\n", line_num1);
   getchar();
//  if (containarray == NULL) exit (1);

   for (n=0; n < line_num1; n++)
      {
      containarray[n] = n;
      }

// // ~ •  •  •  •  •  •  •  •  •  •  •  • Printing Results
   printf ("\n\nYou have entered: ");
   for (n=0;n < line_num1; n++)
   printf ("%i ", containarray[n]);
   printf("\n\n");
// // ~ •  •  •  •  •  •  •  •  •  •  •  • Printing Results

   free (containarray);
}




Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: defining and declaring an array of variable size problems
« Reply #4 on: July 17, 2010, 10:05:08 AM »
BTW: With C99 you can do this
Quote
#include <stdlib.h>
#include <stdio.h>

int foo(int count)
{
   int i, array[count];
   for (i = 0; i< count; i++)
      array[i] = 0;
   printf("count %d\n", i);
   return 0;
}

int main(int argc, char **argv)
{
   int line_count = 42;
   foo(line_count);
   line_count = 54;
   foo(line_count);
   return 0;
}
May the source be with you

EdPellesC99

  • Guest
Re: defining and declaring an array of variable size problems
« Reply #5 on: July 17, 2010, 06:28:30 PM »

Timo,


That is sure nice to know. How sweet is that? Really avoids the need for calloc.


.......Now changing your code a bit to:


Quote

#include <stdlib.h>
#include <stdio.h>
// // ~ http://forum.pellesc.de/index.php?topic=3235.new;topicseen#new With C99 you can do this
int foo(int count)
{
   int i, array[count];
   printf("size of array[count] is %i\n\n", sizeof(array[count]));
   
   for (i = 0; i< count; i++)
   {
   array[i] = i;
   }
   for (i=0; i<count; i++)
      {
   printf("array[%i] = %i\n", i, array[i]);
   }

   return 0;

}

int main(int argc, char **argv)
{
   int line_count = 4;
   foo(line_count);
   line_count = 9;
   foo(line_count);
   return 0;
}


I notice the double pointer:
int main(int argc, char **argv)
if I were to change it to a single pointer, I get the following warning:

Building Timo's Instead of CallocTechnique.obj.
C:\SGV1\C and Structure Use KCards\Read K Index Cards into Array of Structures 3\Timo\Timo's Instead of CallocTechnique.c(23): warning #2181: Incorrect signature for entry-point 'main'; expected 'int __cdecl function(int, char * *)' but found 'int __cdecl function(int, char *)'.


Why is the double pointer necessary? Or should I continue with my studies in "Pointer school" and graduate before I ask such questions?

 :) Thanks for the BTW, that was a gold nugget I would not have found.
Ed


Offline Stefan Pendl

  • Global Moderator
  • Member
  • *****
  • Posts: 582
    • Homepage
Re: defining and declaring an array of variable size problems
« Reply #6 on: July 18, 2010, 12:00:18 AM »
char **argv is an array of strings, where each string consists of an array of char.
---
Stefan

Proud member of the UltraDefrag Development Team