0

Why I am getting error like, expected identifier before '(' before = token in the line: rep -> current = arr[i]?

typedef struct countOf 
{
    int current;
    int count;
    struct countOf *next_ptr;
} countOf;

typedef countOf *rep;

int main()
{ 
    int arr[9];
    int count = 10;
    rep = (struct countOf*) malloc(count* sizeOf(countOf));
    for (i = 0; i < count; i++ )
      {
        rep -> current = arr[i];
        rep = rep->next_ptr;

      }
}

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Ams
  • 21
  • 1
  • 4

3 Answers3

0

In this line

rep = (struct *countOf) malloc(count* sizeOf(countOf));

there are two errors. The first one is that rep is a type name not an object. The second one is that instead of struct *countOf there should be at least struct countOf *. though this casting is redundant

Pay attention to that if you will rewrite this statement correctly

rep = rep->next_ptr;

substituting the type name for a name of a variable nevertheless this statement will invoke undefined behavior because the data member next_ptr was not initialized.

If you want to have a global pointer then you can write for example

rep head;

or

rep head = NULL;

that is the same.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • I want rep to be of type struct countOf* but I want to make it global. So ,what would be the correct syntax? – Ams Apr 13 '20 at 17:29
  • @Ams The correct syntax is exactly like the rest of your variable declarations: `countOf* rep`. Don't use `typedef` here. – Code-Apprentice Apr 13 '20 at 17:30
  • @Ams Also, what are the reasons to declare this variable global? Since it is only used in `main()`, it should be local. – Code-Apprentice Apr 13 '20 at 17:31
0
typedef countOf *rep;

Here you create rep as an alias for countOf*. This means that rep is a type, not a variable.

Later you do this:

rep = (struct countOf*) malloc(count* sizeOf(countOf));

But as I said, rep is a type, so the compiler complains that you are trying to assign a value to it, which doesn't make any sense.

I think you mean instead to do

countOf* rep = (struct countOf*) malloc(count* sizeOf(countOf));

You should also remove the typedef.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
0

Hi Your code has many mistakes and loop holes, I will surely give you a better code but first of all I am just marking mistakes in code and just making your code ready to work. Go through all the comments in code and run this on your machine. This is a working code and help you to understand your mistakes.I checked this on turboC.

#include<stdio.h>
#include<conio.h>
#include<malloc.h>

typedef struct countOf
{
    int current;
    int count;//why??
    struct countOf *next_ptr;//why, u r using continuous blocks
}countOfNode;
// typedef make countOfNode as an dataType
countOfNode *rep;
//Now rep is global pointer of type struct countOf

int main()
{
    int arr[10];//why 9 while we have 10 count everywhere in code
    int count = 10;
    int i;
    rep = (countOfNode*)malloc(count * sizeof(countOfNode));
    printf("Enter 10 values in array if you want to assign value from array structure array");
    for (i = 0; i < count; i++ )
    { clrscr();// to not keep useless on screen;
      printf("Enter %dth value in array",i+1);
      scanf("%d",&arr[i]);
    }
    for (i = 0; i < count; i++ )
      {
      /*any value you want to assign to current
      but what is arr[i]??? it contains no value
    */
      //rep->current = arr[i];
    //Currently there is no next pointer
       // rep = rep->next_ptr;

       //You assign it 10 continuos blocks so what is the use of ptr???
       //For pointer dont assign continuos blocks make it dynamic
       rep[i].current = arr[i];

      }
     printf("Data in structure rep");
     for(i=0 ; i<count; i++)
     {
      printf("%dth node have \t%d\n",i+1,rep[i].current);
     }


}

Now you may check the below code with few improvements in your code, This one is not the best way, but it will help you to better understand. We can write code in far better way

#include<stdio.h>
#include<conio.h>
#include<malloc.h>

typedef struct countOf
{
    int current;
    struct countOf *next_ptr;//it keep address of next node
}countOfNode;

countOfNode *rep;
//Now rep is global pointer of type struct countOf we just keep offset(base address in this)

int main()
{
    int count = 10;
    int i;
    countOfNode *tracker, *tmp;
    rep = (countOfNode*)malloc(sizeof(countOfNode));//initializing first time
    printf("Enter value To store");
    scanf("%d",&rep->current);//storing value for first node
    rep->next_ptr = NULL;//assigin null becaus there is no next node
    tracker = rep; // we move tracker alway, rep will just keep the offset
    for(i=1; i<count; i++)//0 already assigned above
    {
      tmp = (countOfNode*)malloc(sizeof(countOfNode));//initializing current node
      printf("Enter value To store");
      scanf("%d",&tmp->current);//storing value for first node
      tmp->next_ptr = NULL;//assigin null becaus there is no next node
      tracker->next_ptr = tmp; // we move tracker alway, rep will just keep the offset
      tracker = tmp;//tracker jump to next node
    }


    printf("Data in structure rep");
    tracker = rep;
    for(i=0; i<count; i++)//0 already assigned above
    {
       printf("%d\t",tracker->current);
       tracker = tracker->next_ptr;//jump to next
    }
    getch();

}
SayAz
  • 751
  • 1
  • 9
  • 16