The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Declaring an array in the middle of program
Discuss Declaring an array in the middle of program in the C Programming forum on Dev Shed. Declaring an array in the middle of program C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 2nd, 2005, 04:02 PM
|
|
Registered User
|
|
Join Date: Feb 2005
Posts: 7
Time spent in forums: 2 h 41 m 43 sec
Reputation Power: 0
|
|
|
Declaring an array in the middle of program
Hi all,
I'm having a problem with a small program that I have. I have a data file containing a column of numbers...I've been able to basically read this column with fscanf and find out how many elements there are, but in my program, I want to declare an array, say
float MyArray[Number_of_Elements];
and I put this in the middle of the program, after i've fscanf the file, but sure enough it doesn't work. Can anyone help? THANKS!
-Rob
|

February 2nd, 2005, 04:10 PM
|
|
|
try:
Code:
float *p=NULL
.........
p=malloc(sizeof(float) * num_of_columns);
...............
for(i=0;i<num_of_columns; i++)
{
printf("%f\n",p[i]);
}
free(p);
You can reference p[2] or p[3] just like an array element.
Don't do p++ or p-- like you might with other pointers. Instead if you want to use 'regalar' pointer notation try
Code:
float *q;
..........
q=p;
i=0;
while(i<num_of_columns)
{
printf("%f\n", q++);
i++;
}
|

February 2nd, 2005, 04:40 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
In C++, it is completely legal to declare variables anywhere in a code block, whereas in C, you can only declare them at the top of any block. If you do want to declare a variable in the middle of a function in C, you should look at the discussion thread about Anonymous Scopes that we had yesterday. Basically, all you need to do is open a code block and declare the variable within that.
The following code will work in C++, not in C.
Code:
void somefunc() {
int foo;
/* do some stuff here */
foo = dosomething();
/* Now we want to declare an array here */
char array[50]; // This is legal in C++, not in C
/* do something with array */
}
If you want to make it work in C, just open a new code block. For example:
Code:
void somefunc() {
int foo;
/* do some stuff here */
foo = dosomething();
/* Now we want to declare an array here, so open a new block */
{
char array[50]; /* This is legal in C++ and also C, since the variable
is declared at the top of the block */
/* do something with array */
}
/* Do rest of code. array falls out of scope here though */
}
__________________
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne
|

February 2nd, 2005, 05:15 PM
|
 |
Contributing User
|
|
|
|
Well, it's a little muddier than that. - In C++ and C99 you can declare a fixed-size array anywhere, but in C89 you cannot.
- In C89 you cannot declare an array with a variable, even if it is const.
- In C++ you can declare an array with with a variable, but it must be const.
- In C99 you can declare a variable length array using a non-const variable.
I hope I got that right. Quote: | Originally Posted by RguyK128 I've been able to basically read this column with fscanf and find out how many elements there are, but in my program, I want to declare an array, say
float MyArray[Number_of_Elements];
and I put this in the middle of the program | So unless RguyK128 is using C99, dynamic allocation seems to be what he's after.
|

February 2nd, 2005, 06:46 PM
|
|
Registered User
|
|
Join Date: Feb 2005
Posts: 7
Time spent in forums: 2 h 41 m 43 sec
Reputation Power: 0
|
|
|
I'm not sure what C i'm using, I just know it's basic C and not C++, so i'll try with the dynamic allocation that Jim had mentioned. Thanks everyone!
|

February 3rd, 2005, 06:41 PM
|
 |
Last Day: May 29, 2005
|
|
Join Date: Sep 2004
Location: elemental sphere
Posts: 742
 
Time spent in forums: 5 Days 7 h 20 m 20 sec
Reputation Power: 9
|
|
|
What a mess of standards, you'd go crazy if you were to remember all the rules.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|