-3

i need to generate a random number without using "random" function so i found this code which give me mlliseconds in system:

#include <stdio.h>
#include <sys/timeb.h>

int main()
{
struct timeb tmb;

ftime(&tmb);
printf("tmb.time     = %ld (seconds)\n", tmb.time);
printf("tmb.millitm  = %d (mlliseconds)\n", tmb.millitm);

return 0;
}

it was working good until i tried to use it inside struct in this way :

#include <sys/timeb.h>
#include "stdlib.h"
struct Num{
struct timeb tmb;
ftime(&tmp);    
};
int main()
{

return 0;
}

its giving an error at {ftime}. any help ?

1 Answers1

0

ftime(&tmp); is an example of a postfix expression as specified by C11/6.5.2 (fourth line down):

        postfix-expression:
                 primary-expression
                 postfix-expression [ expression ]
                 postfix-expression ( argument-expression-listopt )
                 postfix-expression . identifier
                 postfix-expression -> identifier
                 postfix-expression ++
                 postfix-expression --
                 ( type-name ) { initializer-list }
                 ( type-name ) { initializer-list , }
         argument-expression-list:
               assignment-expression
               argument-expression-list , assignment-expression

The C standard reference specifies the grammar for the struct specifier you've used in C11/6.7.2.1, and clearly your example violates that grammar, as there are no possibilities for a postfix-expression here:

        struct-or-union-specifier:
                 struct-or-union identifieropt { struct-declaration-list }
                 struct-or-union identifier
        struct-or-union:
                struct
                union
        struct-declaration-list:
                struct-declaration
                struct-declaration-list struct-declaration
        struct-declaration:
                specifier-qualifier-list struct-declarator-listopt ;
                static_assert-declaration
        specifier-qualifier-list:
                type-specifier specifier-qualifier-listopt
                type-qualifier specifier-qualifier-listopt
        struct-declarator-list:
                struct-declarator
                struct-declarator-list , struct-declarator
        struct-declarator:
                declarator
                declaratoropt : constant-expression

The syntactic structure of C isn't too difficult to learn; it's taught fairly well within most books. My suggestion is that you find a new guide, as the semantics of C will prove extremely challenging if you struggle with the syntax. As I wrote in the comments for your question:

I suggest K&R2E; do the exercises as you come across them (and don't move on 'til they're complete).

If you struggle with some words from your textbook or an exercise, please post a question about whatever it is that confuses you.

autistic
  • 1
  • 3
  • 35
  • 80