Author Topic: [C] Messing around can't figure outthis small issue  (Read 520 times)

0 Members and 1 Guest are viewing this topic.

Offline jitterbud

  • /dev/null
  • *
  • Posts: 8
  • Cookies: 0
    • View Profile
[C] Messing around can't figure outthis small issue
« on: May 20, 2015, 09:03:38 pm »

Random stuff to learn how code operates. I get this error but I don't know what I'm doing wrong. Anyone care to explain what the issue may be and point me into some resources to better understand. THANKS!
ponter.c:5:3: error: expected identifier or ‘(’ before ‘{’ token
   {

Code: [Select]
#include <stdio.h>


typedef struct  {
             char *testinput;
             int i;           
        } labrat;

int main()
{
   
    int z;
        void addone(int z);
        {
        z++;
        }   
    int n,a,x=1;

    int *pointer_to_a = &a;
    int *pointer_to_n = &n;
    labrat test;
    test.testinput="THis";
    test.i = 5;
    a+=1;
    *pointer_to_a+=1;
    scanf("Enter N value N is %d\n", pointer_to_n);
    printf("THe value of n is %d\n", *pointer_to_n);
    printf("N value is now %d\n", n);   
    printf("THe value of %d\n", a);
    printf("The value of a is also %d\n", *pointer_to_a);
   
    printf("%s is boring %d\n", test.testinput, test.i);
    printf("%d is befor", x);   
    addone(z);
    printf("%d is now", x);
       

}
/*###############################POINTERS#################################*/


Offline jitterbud

  • /dev/null
  • *
  • Posts: 8
  • Cookies: 0
    • View Profile
Re: [C] Messing around can't figure outthis small issue
« Reply #1 on: May 20, 2015, 09:26:04 pm »
Code: [Select]
struct  labrat{
             char *testinput;
             int i;           
        }test;

Googling Structures....

Offline Xires

  • Noob Eater
  • Administrator
  • Knight
  • *
  • Posts: 379
  • Cookies: 149
    • View Profile
    • Feed The Trolls - Xires
Re: [C] Messing around can't figure outthis small issue
« Reply #2 on: May 21, 2015, 11:59:21 am »
Code: (c) [Select]
typedef struct {
  char *testinput;
  int i;
} labrat;

The above is perfectly valid.  C permits 'anonymous structures', which are structs which have no name, when you create a variable of that type(in this case, 'labrat').  By mixing it with 'typedef', you're declaring that 'labrat' is a type name.  It is functionally equivalent to the following:

Code: (c) [Select]
struct testrat {
  char *testinput;
  int i;
};

typedef testrat labrat;

@OP; your primary issue is the fact that you have a semicolon after your nested function declaration: "void addone(int z);".  This is the problem that the compiler is complaining about.

Other notes:

ISO C 'forbids'(meaning recommends against) nested function declarations, but most compilers should still compile just fine with the nested 'addone()' function.  Also, realize that because you're not passing a pointer to this function, 'addone()' will not actually be making changes to the source variable since a copy of the variable is made(to stack space).  Another issue you're going to have, in runtime, is with the assignment of "THis" to test.testinput.  You need to do dynamic allocation to allocate space for this variable to store its data and use something like strncpy() to copy the string literal "THis" to the allocated memory(heap space).  The structure of your code could use some work and the "%d is now" may be preferred as "x is now %d".

It may take a while, but keep at it.  Progress is made with each step.
-Xires