Solved

Pointers in C

Posted on 2003-10-29
6
248 Views
Last Modified: 2010-04-17
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
Comment
Question by:LeelaSwamy
6 Comments
 
LVL 8

Expert Comment

by:harris_c
Comment Utility
Assignment?
0
 
LVL 2

Accepted Solution

by:
mrd_2k earned 25 total points
Comment Utility
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
 
LVL 1

Assisted Solution

by:SergioSaraiva
SergioSaraiva earned 25 total points
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now