C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 2nd, 2005, 04:02 PM
RguyK128 RguyK128 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 7 RguyK128 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #2  
Old February 2nd, 2005, 04:10 PM
jim mcnamara jim mcnamara is offline
......@.........
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jun 2004
Posts: 1,345 jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 4 Days 39 m 18 sec
Reputation Power: 55
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++;
}

Reply With Quote
  #3  
Old February 2nd, 2005, 04:40 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,406 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 9 h 35 m 45 sec
Reputation Power: 4080
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

Reply With Quote
  #4  
Old February 2nd, 2005, 05:15 PM
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Apr 2004
Posts: 1,676 Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Month 3 Days 8 h 23 m 46 sec
Reputation Power: 132
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.

Reply With Quote
  #5  
Old February 2nd, 2005, 06:46 PM
RguyK128 RguyK128 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 7 RguyK128 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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!

Reply With Quote
  #6  
Old February 3rd, 2005, 06:41 PM
nnxion's Avatar
nnxion nnxion is offline
Last Day: May 29, 2005
Dev Shed Novice (500 - 999 posts)
 
Join Date: Sep 2004
Location: elemental sphere
Posts: 742 nnxion User rank is Lance Corporal (50 - 100 Reputation Level)nnxion User rank is Lance Corporal (50 - 100 Reputation Level)nnxion User rank is Lance Corporal (50 - 100 Reputation Level) 
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Declaring an array in the middle of program

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap