0

This is my code that I am compiling in C. Currently I have a global variable 'code' that is an array of structs(struct instruction). I've been trying to instead make this a local variable in main and pass it as a parameter. Also I believe this means I will need to have read file return a struct instruction*. I would greatly appreciate it if someone could explain, or show me how to properly use 'code' as a local variable. Also I am interested in what makes local variables better or more efficient than global variables. Thanks!

#include<stdio.h>
#include <stdlib.h>


typedef struct instruction{
 int op; //opcode
 int  l; // L
 int  m; // M
} instr;

FILE * ifp; //input file pointer
FILE * ofp; //output file pointer
instr code[501];

void read_file(instr code[]);
char* lookup_OP(int OP);
void print_program(instr code[]);
void print_input_list(instr code[]);


int main(){

 read_file(code);
 print_input_list(code);//used for debugging
 print_program(code);

}

void read_file(instr code[]){
 int i = 0;

 ifp = fopen("input.txt", "r");

 while(!feof(ifp)){
    fscanf(ifp,"%d%d%d",&code[i].op, &code[i].l, &code[i].m);
    i++;
 }
 code[i].op = -1; //identifies the end of the code in the array
 fclose(ifp);
}
slinhart
  • 562
  • 5
  • 17

3 Answers3

0

You have to move your declarations inside the functions that need them:

#include <stdio.h>
#include <stdlib.h>


typedef struct instruction{
 int op; //opcode
 int  l; // L
 int  m; // M
} instr;

void read_file(instr code[]);
char* lookup_OP(int OP);
void print_program(instr code[]);
void print_input_list(instr code[]);


int main(){

  instr code[501];  // code[] declaration moved HERE!!!!

 read_file(code);
 print_input_list(code);//used for debugging
 print_program(code);

}

void read_file(instr code[]){
 int i = 0;

 FILE * ifp; //ifp FILE* declaration moved HERE!!!!

 ifp = fopen("input.txt", "r");

 while(!feof(ifp)){
    fscanf(ifp,"%d%d%d",&code[i].op, &code[i].l, &code[i].m);
    i++;
 }
 code[i].op = -1; //identifies the end of the code in the array
 fclose(ifp);
}

I've moved ifp declaration inside readfile() and code inside main(). The variable ofp has been removed, because it is not used.
If you are using ofp inside another function, declare it there.

pablo1977
  • 4,281
  • 1
  • 15
  • 41
  • Hey pablo, thank you this all makes sense. But why is it that you can change the contents of code in read_file without having to return anything? Is this a property of arrays? An explanation clearing that up would be awesome! – slinhart Sep 12 '13 at 19:13
  • @studmac Because `code` is an array, which is essentially a pointer to the start of the memory location. So, essentially you are passing a pointer. Passing a pointer to the array allows the functions to mutate the array in place. – Freddie Sep 12 '13 at 19:18
  • @freddie Why is it then that when you have a function doing an operation on a linked list, you still have to return the head? Isn't this the same concept as the arrays? Since you pass in (some_struct* head), and then have to return the head at the end of the function. – slinhart Sep 12 '13 at 21:16
  • @studmac It depends on what the function does. If you are just modifying some info in the node, you shouldn't need to return anything (just like in the array example). But if you were changing the head node pointer, you'd need to actually pass a pointer to the pointer of the head. I would recommend that you take some time and read section 5.2 of The C Programming Language by Kernighan and Ritchie. If you do a Google search, I'm sure you can find it. – Freddie Sep 12 '13 at 22:00
0

Simple enough. No real change in efficiency as you have currently coded it. The only change is the storage for code will be from the stack

int main(){ 
 instr code[501];

 read_file(code);
 print_input_list(code);//used for debugging
 print_program(code);
}
Sanjaya R
  • 6,246
  • 2
  • 17
  • 19
0

I'll go ahead and try to answer the last part of the question:

Also I am interested in what makes local variables better or more efficient than global variables.

There are a few differences between Local and Global defined variables.

  1. Initialization. Global variables are always initialized to zero, where as local variables will have an unspecified/indeterminate value prior to being assigned. They don't have to be initialized as stated previously.
  2. Scope. Global variables can be accessed by any function in the file (and even out of the file by using extern, without passing a reference to it. So, in your example you didn't need to pass a reference to code to the functions. The functions could have just accessed it normally. Local variables are defined only in the current block.

For example:

int main() {
  int j = 0;
  {
    int i = 0;
    printf("%d %d",i,j); /* i and j are visible here */
  }
  printf("%d %d",i,j); /* only j is visible here  */
 }

This would not compile, because i is no longer visible in the main code block. Things could get tricky when you have global variables named the same as local variables. It's allowed but not recommended.

Edit: Local variable initialization changes based on comments. Changed text in italics above.

Freddie
  • 871
  • 6
  • 10
  • "... local variables **have to be** initialized to a given value before accessing them." Technically this is not true (although it is often a sensible thing to do). For example you **don't have to** initialize a local variable that will be used to receive the data read by `scanf`. Local uninitialized variables value is, according to the standard, unspecified. Whether or not you **should** initialize a local variable is not mandated by the standard. – LorenzoDonati4Ukraine-OnStrike Sep 12 '13 at 20:25
  • @LorenzoDonati Yes, you are correct. Just note, that local variables will have an indeterminate value until assigned. – Freddie Sep 12 '13 at 21:06
  • That's exactly what "unspecified" in C standard means (see [here](http://stackoverflow.com/questions/18420753/unspecified-undefined-and-implementation-defined-behavior-wiki-for-c/18420754#18420754)). – LorenzoDonati4Ukraine-OnStrike Sep 12 '13 at 22:12