-1

I am programming a simple text editor in C. I have to define inuse_head and free_head as global variables. I need to change the value of the 2 global variables in a function. Here is the code I wrote so far:

#include <stdio.h>
#include <string.h>

struct node
{
    char statement[40];
    int next;
};
struct node textbuffer[25];

int free_head;
int inuse_head;

void insert(int line, char* stat)
{
    FILE *file;
    file=fopen("deneme.txt","a");
    
    if(file!=NULL)
    {
        
        int i;
        int k;
        
        strcpy(textbuffer[line].statement,stat);
        textbuffer[line].next=line+1;
        fprintf(file,textbuffer[line].statement);
        
        for(i=0;i<=25;i++)
        {
            if(textbuffer[i].statement==NULL)
            {
                free_head=i;
                break;
            }
        
        }
        
        for(k=0;k<=25;k++)
        {
            if(textbuffer[k].statement!=NULL)
            {
                inuse_head=k;
                break;
            }
        
        }
    
    }
    
    else
    {
        printf("File couldn't found.");
    }
    fclose(file);

}


int main()
{
    insert(3,"Hello World");
    printf("free list: %d and inuse list: %d",free_head,inuse_head);
    return 0;   
}

Now when I print free_head and inuse_head, it prints 0 for both of them. I need to change free_head's and inuse_head's values in function insert. I think I should handle it with pointers but how?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Oğulcan
  • 17
  • 4
  • Have you tried running your code line by line in a debugger while monitoring the values of all variables, in order to determine at which point your program stops behaving as intended? If you did not try this, then you probably want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) You may also want to read this: [How to debug small programs?](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Andreas Wenzel May 28 '21 at 19:07
  • 1
    The code contains *undefined behaviour* in `for(i=0;i<=25;i++) if(textbuffer[i].statement==NULL) ...` because the index 25 is out of range of `struct node textbuffer[25];`. – Weather Vane May 28 '21 at 19:15
  • 1
    This `if(textbuffer[i].statement == NULL)` will never be true. The `textbuffer[i].statement` is a fixed array which decays to a pointer, which is not `NULL`. Did you mean `if(strcmp(textbuffer[i].statement, "") == 0)` ? – Weather Vane May 28 '21 at 19:30

1 Answers1

0

The reason why both variables have the value 0 is not that the function insert is having trouble accessing these variables. Since the variables are both global, the function insert can access both variables directly, like any other variable. It is therefore not necessary to use pointers for accessing these variables.

The problem is rather that in the function insert, the line

free_head=i;

never gets executed, and the line

inuse_head=k;

is only executed once, at a time when k has the value 0.

That is why both variable's values never change their values and both keep their initial value of 0 throughout the entire program.

Also, it is worth noting that your program has undefined behavior. As already pointed out in the comments section, you are accessing the array textbuffer out of bounds.

The line

for(i=0;i<=25;i++)

should be changed to

for(i=0;i<25;i++)

and the line

for(k=0;k<=25;k++)

should be changed to:

for(k=0;k<25;k++)

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39