• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 295
  • Last Modified:

Pointers in C

1) Why pointers are used apart from the adv that they r very useful during dynamic memory allocation?
2) What is the use of function pointers?
3) What is the difference between static functions and ordinary functions? how r static functions useful?
4) Can u change the value of a macro dynamically?
5) What is #pragma? At what circumstances it should be used?
6) How to invoke someother user defined function before main?
Eg: void func() { printf("Hi" ); }
void main() { printf("Hello" ); }

i want the o/p as Hi Hello. how to invoke func() before main()
     
0
LeelaSwamy
Asked:
LeelaSwamy
2 Solutions
 
harris_cCommented:
Assignment?
0
 
mrd_2kCommented:
1) Why pointers are used apart from the adv that they r very useful during dynamic memory allocation?
You can traverse arrays, reference and manipulate data within a larger body, pass references to large amounts of data that would be slow to copy
2) What is the use of function pointers?
use your imagination ;) you can have multiple functions with the same prototype, but different functionality invoked via a pointer.
3) What is the difference between static functions and ordinary functions? how r static functions useful?
static CLASS MEMBER functions don't have a 'this' pointer, can't access instance data. however they can be invoked as a normal function is, without a cooresponding class instance.
4) Can u change the value of a macro dynamically?
im not quite sure what you mean. macros generate code, before the code is actually compiled
5) What is #pragma? At what circumstances it should be used?
pragmas are compiler-specific directives that perform specific functions not addressed in the language standards. alot of machine and os-specific functionality is addressed
6) How to invoke someother user defined function before main?
Eg: void func() { printf("Hi" ); }
void main() { printf("Hello" ); }
you shouldn't ever need to. main() signifies your program's entry point. you should build your code around it.

0
 
SergioSaraivaCommented:
Is this homework?

Some of this answers where simple copy-paste from msdn (http://www.msdn.com), as I could not make better...


1) Why pointers are used apart from the adv that they r very useful during dynamic memory allocation?

Pointers are the most powerful (and difficult) way to manipulate memory data. You can use pointers to change values by reference, to travel through arrays (continuous memory block), etc

2) What is the use of function pointers?

Imagine you want to implement a generic sort function. This function should sort an array of any type. For that you will need the array, the type size, and a function which compares two values of the type specified. Then, your sort function should use this “user defined” function to compare each value of the array, in order perform the algorithm. Each time you want to sort a new type of data, all you have to is to implement this compare function and pass it to the sort function (which does not need to be touched).
To pass the compare function to the sort function, you can use a function pointer.

3) What is the difference between static functions and ordinary functions? how r static functions useful?

Static member functions are considered to have class scope. In contrast to nonstatic member functions, these functions have no implicit this argument; therefore, they can use only static data members, enumerators, or nested types directly.

4) Can u change the value of a macro dynamically?

No. Macros are expanded before the code is actually compiled.

5) What is #pragma? At what circumstances it should be used?

Each implementation of C and C++ supports some features unique to its host machine or operating system. Some programs, for instance, need to exercise precise control over the memory areas where data is placed or to control the way certain functions receive parameters. The #pragma directives offer a way for each compiler to offer machine- and operating system-specific features while retaining overall compatibility with the C and C++ languages. Pragmas are machine- or operating system-specific by definition, and are usually different for every compiler.

6) How to invoke someother user defined function before main?
Eg: void func() { printf("Hi" ); }
void main() { printf("Hello" ); }

i want the o/p as Hi Hello. how to invoke func() before main()


There is no way of doing that. And if you ever watch something similar, close you eyes not to see it…

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now