Solved

Pointers in C

Posted on 2003-10-29
6
259 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
ID: 9647838
Assignment?
0
 
LVL 2

Accepted Solution

by:
mrd_2k earned 25 total points
ID: 9648063
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
ID: 9648938
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A short article about a problem I had getting the GPS LocationListener working.
This is an explanation of a simple data model to help parse a JSON feed
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 …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

867 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

25 Experts available now in Live!

Get 1:1 Help Now