What is the function of fflush(stdin)?
What for the operator -> used?

Recommended Answers

All 20 Replies

> What is the function of fflush(stdin)?
It's undefined
It's undefined

> What for the operator -> used?
Do you have a book?
It's used when you have a pointer to a struct, so rather than writing (*p).member, you can write p->member, presumably in a less error prone way.

What is the function of fflush(stdin)?
What for the operator -> used?

->this operator is used to access the member of structure or union or class through the pointer variable for eg
struct student
{
int b;
};
void main()
{
student var1,*a;
a=&var1;
printf("%d",var1->b);
}

Ugh, what an awful example.

>void main()
int main ( void ). No excuses, and the only portable return values are 0, EXIT_SUCCESS, and EXIT_FAILURE (defined in stdlib.h). Returning nothing isn't an option.

>student var1,*a;
While your C++ compiler might accept this, the C language requires that you include the struct keyword in structure variable declarations.

>printf("%d",var1->b);
This is a doozy. You forgot to include stdio.h, and thus the entire program is undefined. You're also mixing up the variables you just defined. It should be a->b, because var1 isn't a pointer. Finally, you never initialized var1, so the contents of b are indeterminate and printing it is undefined.

It's a wonder your code even compiles for you at all, but I'm willing to bet you just wrote it from memory (and your memory isn't that good). This is the correct example:

#include <stdio.h>

struct student {
  int b;
};

int main ( void )
{
  struct student var1 = {123};
  struct student *a = &var1;

  printf ( "%d\n", a->b );

  return 0;
}

Thanks to all for clearing my doubts.


>student var1,*a;
While your C++ compiler might accept this, the C language requires that you include the struct keyword in structure variable declarations.

I always had this doubt. Why to use 'struct' keyword? As, i am using C++ compiler, i didnt face any problem.

One more thing,

Why have you used curly braces :

struct student var1 = {123};

Thanks to all for clearing my doubts.

I always had this doubt. Why to use 'struct' keyword? As, i am using C++ compiler, i didnt face any problem.

One more thing,

Why have you used curly braces :

as srtuct is the keyword and it includes in the syntax of creating the userdefined datatype structure and it also vary from compiler to compiler as in c++
compiler we need not to write class for making its objects


i didnot get ur second doubt about some brackets
pls be specified and reply again

@narue
Wow, that's a nice explaining there. Which compiler do you use? While, we are here asking about functions, can I ask which would a replacement for getch(); for C. I have read through some of the threads here, stating that it's an compiler extension. What code should we put to halt the console screen in an IDE interface?

@Koyong
Please use the code snippets, it's really annoying reading these, and we can't understand the text.
Also, while declaring variables like option try going for global static it'll be much easier to access.

The very first error is simple and like total basic. in Line no. 63 you have to use %d to get a data value in scanf(); Also while getting the detalis into the file using the struct construct use the typedef you declared earlier. Like aExpenses.structureVariable other wise it'll just show you a break error that, it's not declared. I haven't gone through the whole program in detail yet, cause it's so effin' hard to read. so wait a bit more.

You are also in the very forst function of INPUT where is it, that we actually entering the data? you are just reading it..


i didnot get ur second doubt about some brackets
pls be specified and reply again

while initializing the variable var1, the code used is (pls. refer the code given below):

struct student var1 = {123};

#include <stdio.h>

struct student {
  int b;
};

int main ( void )
{
  struct student var1 = {123};
  struct student *a = &var1;

  printf ( "%d\n", a->b );

  return 0;
}

I simply wanted to know, is there any specific reason in using 123 as {123}?

The curly brackets are used to initialize each member of a structure. you are not using struct student var1 = {123}; to declare a variable. It's a structure, so it must be passes with the {}. A structure can have 'n' number of data types, so to specify each of them, we use {} curly brackets.

Here is an Example:

struct proddata {
char prodname[8];
int no_of_sales;
float total_sale;
};

This is your general declaration of a structure with the label "proddata"

struct proddata example = {"Coal",0,0};

There is your initialization of structure with the label proddata, and the name "example". So, as you can see, we are assigning three variables here. So, since Narue used only a single variable inside a structure, but this is the standard syntax for declaring a structure. Remember, it's a user-defined. If you want to use just 123 then you have to point it to the variable.

struct proddata example.no_of_sales = 123;

Do you understand. We use the curly brackets to assign values to the whole structure. (BTW, thx.. trying to explain you this I myself understood how it works..) So , like if there is any problem with what I understood here, please point it out.

>Why to use 'struct' keyword?
Put simply, without the struct keyword the compiler doesn't know if you're referring to a type or a variable. The assumption is that it's a variable, and the symbol lookup fails. You don't need to do this with C++ because C++ automagically creates a typedef for you that omits the keyword. You can do the same thing in C, if you want:

typedef struct foo {
  int far;
} foo;

struct foo var2; /* Okay */
foo var1;        /* Also okay */

This works because typedef names and structure tags are in a separate name space, so they won't collide with each other.

>Why have you used curly braces
It's an initialization list, just like an array. The items wrapped in braces initialize the members of the structure in the order that they're defined. If there aren't enough items to initialize all of the members, the rest of the members are initialized to the type's representation of 0:

int var1[3] = {1, 2, 3};

struct foo {
  int a;
  int b;
  int c;
};

struct foo var2 = {1, 2, 3};

Both of the initialization lists are equivalent (more or less) to this:

var1[0] = 1;
var1[1] = 2;
var1[2] = 3;

var2.a = 1;
var2.b = 2;
var2.c = 3;

>Which compiler do you use?
I use several compilers across several operating systems. I recommend you also use more than one compiler, as it's a good practice for improving the portability of your code.

>While, we are here asking about functions, can I
>ask which would a replacement for getch(); for C.
Unfortunately, there isn't one. getch performs a task that the C standard really can't specify without hurting the portability of the language as a whole. To simulate it, you basically have to write your own using system-specific tools. What OS and compiler do you use?

...Do you understand. We use the curly brackets to assign values to the whole structure. ..

Thanks a lot. Its clear to me now.

It means, in the following example

#include <stdio.h>

struct student {
  int b;
};

int main ( void )
{
  struct student var1 = {123};
  struct student *a = &var1;

  printf ( "%d\n", a->b );

  return 0;
}
struct student var1 = {123};

may also be written as

struct students var1.b = 123;

@ksri
Correct. It basically depends upon you how you want to initiate a variable. one by one or as whole(struct)

@Narue
>What OS and compiler do you use?
Well, I tried using different compilers too, to get a hang of different environments and improve my skills. I'm using Windows Vista Ultimate, so not many compilers that I know is supported here. I tried Dev-C++ in Vista but didn't worked. But I practice with Dev-C++ and Gpp compiler in my class. Here at home, I can only get Visual Studio 2008 express to work.
I also heard that Linux was a good operating system so I tried Ubuntu, but I didn't understand much so sadly had to uninstall it. (of course it doesn't have the C compiler-_-)

>struct student var1 = {123};
>may also be written as
>struct students var1.b = 123;
No, because the "struct student" part is defining a variable. You can't access the members of that variable directly using the dot operator until it's completely defined. This would work though:

struct student var1;

var1.b = 123;

>Here at home, I can only get Visual Studio 2008 express to work.
Okay, so you're looking at a Windows based solution. Simulating getch is actually pretty easy:

#include <stdio.h>
#include <windows.h>

void RawInput ( BOOL on )
{
  HANDLE in = GetStdHandle ( STD_INPUT_HANDLE );
  DWORD mode;

  GetConsoleMode ( in, &mode );

  if ( on )
    mode &= ~ENABLE_LINE_INPUT;
  else
    mode |= ENABLE_LINE_INPUT;

  SetConsoleMode ( in, mode );
}

int main ( void )
{
  RawInput ( TRUE );

  /* getchar won't wait for a newline */

  RawInput ( FALSE );

  /* getchar will wait for a newline */

  return 0;
}

You can do the same thing in Linux with the same technique:

#include <termios.h>
#include <unistd.h>
#include <stdio.h>

int getch ( void )
{
  struct termios term, save;
  int c;

  /* Get the old terminal and save it */
  tcgetattr ( STDIN_FILENO, &term );
  save = term;

  /* Enter non-canonical mode */
  term.c_lflag &= ~ICANON;
  term.c_lflag &= ~ECHO;
  term.c_cc[VMIN] = 1;
  term.c_cc[VTIME] = 0;

  /* Apply the new settings */
  tcsetattr ( STDIN_FILENO, TCSANOW, &term );

  /* getch! */
  c = getchar();

  /* Restore the original terminal */
  tcsetattr ( STDIN_FILENO, TCSANOW, &save );

  return c;
}

>of course it doesn't have the C compiler-_-
To the best of my knowledge, all Linux distros come with GCC.

>No, because the "struct student" part is defining >a variable
OOps... I really should have mentioned that. My bad. Any variable befofe using must be declared. as struct is a user-defined value type data-type, which contains different data-types. So we need to first tell the compiler to create a var1 structure first, before using any of it's variables.

>To the best of my knowledge, all Linux distros >come with GCC.
Well all but the ubuntu. well for some reason it's not there in the default installation, and you have to download it separately. Which Linux edition would you recommend for a beginner? Ubuntu is easy and fun, and with near similar interface as windows so it'll be easy. but now I really don't want to do partitions and install it again, just to remove it.

>Which Linux edition would you recommend for a beginner?
I'm not a hip Linux afficionado, so I'd recommend Mandrake (it's called Mandriva now though). It's easy to install and use, offers a live CD version, and has served me well. Another one you might take a look at is Knoppix. I use that for troubleshooting machines that won't boot into Windows.

>you might take a look at is Knoppix
Yes I tried Knoppix before, it's a nice live CD version. I'll try to run it off the Virtual PC, if it fails I'll do the live CD

Ugh, what an awful example.

>void main()
int main ( void ). No excuses, and the only portable return values are 0, EXIT_SUCCESS, and EXIT_FAILURE (defined in stdlib.h). Returning nothing isn't an option.

>student var1,*a;
While your C++ compiler might accept this, the C language requires that you include the struct keyword in structure variable declarations.

>printf("%d",var1->b);
This is a doozy. You forgot to include stdio.h, and thus the entire program is undefined. You're also mixing up the variables you just defined. It should be a->b, because var1 isn't a pointer. Finally, you never initialized var1, so the contents of b are indeterminate and printing it is undefined.

It's a wonder your code even compiles for you at all, but I'm willing to bet you just wrote it from memory (and your memory isn't that good). This is the correct example:

#include <stdio.h>

struct student {
  int b;
};

int main ( void )
{
  struct student var1 = {123};
  struct student *a = &var1;

  printf ( "%d\n", a->b );

  return 0;
}

is that correct?????

>is that correct?????
Yes, the example is absolutely correct. Why don't you check it yourself before posting it unnecessarily. and you didn't had to quote the WHOLE message with it. I wonder if you even tried to compile the code to see if it works or not >_>. Stop wasting time.

>is that correct?????
To be perfectly frank, I'm usually correct. If you aren't sure, it's safe to assume that everything I tell you is absolutely accurate.

is that correct?????

No -- Narue lied to you. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.