GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 28-Apr-2008, 18:12
contherun contherun is offline
New Member
 
Join Date: Apr 2008
Posts: 18
contherun is on a distinguished road
Question

How to give size to this array?


CPP / C++ / C Code:
#include <stdio.h>
 
void call();
void send(int);
 
void main()
{
    call();
}
 
void call( )
{
    int x = 6;
    send(6); //passing the size of the array as an argument
}
 
void send(int i)
{
    int arr[i]; //gives the error, 
 
}

I want to create an array by passing the size of that array as an argument to
send(). The array must be a static array NOT dynamic. How do i create a
static array like this?
Last edited by admin II : 29-Apr-2008 at 03:47. Reason: Please surround your C code with [cpp] your code [/cpp]
  #2  
Old 28-Apr-2008, 21:39
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,720
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: How to give size to this array?


Quote:
Originally Posted by contherun
I want to create an array by passing the size of that array as an argument to
send(). The array must be a static array NOT dynamic. How do i create a
static array like this?

A little terminology:
By definition, space for an object with static storage duration is provided at compile time (actually it's part of the binary executable file, and storage is allocated at load time). Therefore it is quite impossible to have the size specified by a variable whose value is not known until run time.

The so-called C99 language standard provides for variable-length arrays. That is, arrays that can be declared at run time with a dimension that is a variable, so your example might work with some compilers.

Two things to note:

1. Those arrays are not static arrays. They can't be, by definition of static.

2. Not all compilers support Variable Length Arrays; in fact many commonly used compilers do not. Recent GNU compilers do (so your example could be compiled with GNU compilers, although you would get a warning telling you that the return type of main() is supposed to be an int). Borland and Microsoft compilers to which I have access do not support Variable Length Arrays.


So: What, exactly is the assignment?

Regards,

Dave
  #3  
Old 29-Apr-2008, 00:57
contherun contherun is offline
New Member
 
Join Date: Apr 2008
Posts: 18
contherun is on a distinguished road
Arrow

Re: How to give size to this array?


it's a test program. I just need to pass an integer to a method as an argument and that integer will be the size of an array which is defined in that method.

CPP / C++ / C Code:
main()
{
    method(4);
}

method(int i)
{
     int arr[i];//in this case passed "4" should be the size
}

later i want to assign values to that array to every element in it.
Last edited by LuciWiz : 29-Apr-2008 at 07:30. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
  #4  
Old 29-Apr-2008, 08:28
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,720
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: How to give size to this array?


Quote:
Originally Posted by contherun
I just need to pass an integer

I tried to explain (in the item number '2') that if your compiler supports the C99 standard feature known as Variable Length Arrays, then you can do it. If your compiler does not support Variable Length Arrays, then you can not do it.
CPP / C++ / C Code:
#include <stdio.h>

int foo(int);

int main()
{
    int i;

    printf("Enter a positive integer: ");
    if ((scanf("%d", &i) != 1) || (i <= 0)) {
        printf("Invalid.\n");
        return 0;
    }
    printf("\nCalling foo(%d)\n\n", i);
    foo(i);
    return 0;
}

int foo(int size)
{
    int iarray[size];
    int number_of_ints;
    int i;
    printf("  In foo(): size   = %d\n", size);
    printf("  sizeof(iarray)   = %d\n", sizeof(iarray));
    printf("  sizeof(iarray[0] = %d\n", sizeof(iarray[0]));
    number_of_ints = sizeof(iarray)/sizeof(iarray[0]);
    printf("  Number of ints   = %d\n", number_of_ints);
    for (i = 0; i < number_of_ints; i++) {
        /* whatever */
    }
    return 0;
}
Output after compiling with GNU gcc 3.4.4 or 4.1.2
Code:
Enter a positive integer: 6 Calling foo(6) In foo(): size = 6 sizeof(iarray) = 24 sizeof(iarray[0] = 4 Number of ints = 6

Result of trying to compile with Borland bcc32.exe version 5.82 from Turbo C++ Explorer:
Code:
Error E2313 test_vla.c 21: Constant expression required in function foo Warning W8057 test_vla.c 32: Parameter 'size' is never used in function foo *** 1 errors in Compile ***

Result of trying to compile with cl.exe from Microsoft Visual C++ express 2008:
Code:
test_vla.c(21) : error C2057: expected constant expression test_vla.c(21) : error C2466: cannot allocate an array of constant size 0 test_vla.c(21) : error C2133: 'iarray' : unknown size test_vla.c(24) : warning C4034: sizeof returns 0 test_vla.c(26) : warning C4034: sizeof returns 0

Regards,

Dave
  #5  
Old 29-Apr-2008, 13:46
contherun contherun is offline
New Member
 
Join Date: Apr 2008
Posts: 18
contherun is on a distinguished road
Arrow

Re: How to give size to this array?


Thank you. , Well i use MS C++ compilor. those errors that you have shown occured to me too. Beucase i use MS compilor. Thats the reason why i post this thread. I know the logic seems OK but the compilor doesnt like my code. Therefore the question is how to do it in MS compilor?
  #6  
Old 29-Apr-2008, 13:53
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 509
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: How to give size to this array?


CPP / C++ / C Code:
method(int i)
{
  //int arr[i];//in this case passed "4" should be the size
  int *arr = (int*)malloc(i*sizeof(int));

  // do whatever you need to do here



  // free the memory
  free(arr);
}
  #7  
Old 29-Apr-2008, 14:07
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,720
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: How to give size to this array?


Quote:
Originally Posted by contherun
...the question is how to do it in MS compilor?
If your compiler doesn't support Variable Length Arrays, you can't do it without using a dynamically-allocated array (as fakepoo has demonstrated).

In your first post you specifically said that you are not allowed to use a dynamic array (with a capital NOT). So I guess you are SOL (Somewhat Out of Luck) with that compiler.

Regards,

Dave
  #8  
Old 29-Apr-2008, 14:17
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 509
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: How to give size to this array?


There is a rather sloppy way to do it if you MUST have a static array and you know the maximum value that the variable i will ever be.

For example, suppose we know that i will always be a number between 1 and 100:
CPP / C++ / C Code:
void method(int i)
{ int arr[100]; 

  // validate i
  if(i > 100) return;

  // when looping on arr, do it like
  for(j=0;j<i;++j) arr[j] = 0;

}
I don't condone this sort of method because it wastes memory, but if it needs to be done...
  #9  
Old 29-Apr-2008, 14:46
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,720
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: How to give size to this array?


Quote:
Originally Posted by fakepoo
There is...
1.
A matter of terminology: It's still not static. It goes out of scope when the program returns from the function. However, the Original Poster was later willing to abandon his "static" requirement (I think). If you want any variable (including an array) that is declared inside a function to be static, you have to declare it static. Period. And, as I mentioned previously, you simply can't declare a static array with a variable size (which was his original request) any time, anywhere, in any version of C.

2.
The Original Poster specifically said that the size of the array is to be determined by the function parameter.

I was really, really hoping that the Original Poster would restate the problem in a way that makes sense. In other words, I don't want to guess what the real requirements are. (Maybe a dynamically-allocated array with a static pointer, so that the array could be allocated upon the first call to the function but would be accessible with unchanged contents for subsequent calls. Something like that. But why should we have to guess?)


Regards,

Dave
 
 

Recent GIDBlogDeveloping GUIs with wxPython (Part 3) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
where is the problem and can you fix it (php) oggie MySQL / PHP Forum 8 14-Apr-2008 15:08
Getting a line error in register oggie MySQL / PHP Forum 5 13-Apr-2008 16:16
Need help deleting the last element in the array headphone69 C++ Forum 2 15-Mar-2006 19:31
Having a problem Chuckles Computer Hardware Forum 19 13-Sep-2004 12:17

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 05:47.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.