FAQ Questions

1. How do I dynamically create an array?
2. How do I dynamically create 2D array?
3. How do I resize an array?
4. How to get the length of a AnsiString?
5. Which way is correct- “int* p;” or “int *p;”?
6. How can I declare a generic pointer pointing to a function?
7. Is int *p = 4 valid? What does it mean? ie p = 4 or *p = 4?
8. How can I access two bytes i.e a short value using a char * pointer?
9. Are there void pointers?

FAQ Answers

Q: How do I dynamically create an array?
A: You should use pointers. This way:

int *arr; //it can be any other type (char, float...)
arr = new int[n]; //n should be integer variable

Q: How do I dynamically create 2D array?
A: This way:

int **arr;
int N,M;
cin >> N >> M;

arr = new int*[N];
for (int i=0;i< N; i++) arr[i] = new int[M];

This code will create a 2D array, with NxM dimentions.

Q: How do I resize an array?
A: If you have created a regular array, this way:

int arr[10];

… then, you can’t resize it. Alhough, if you have created an array dynamically, using malloc() then you can use realloc() to resize it.

void *realloc( void *memblock, size_t size );

As “memblock” you should pass the array’s name, and as “size”- the new size. Include stdlib.h .

Q: How to get the length of a AnsiString?
A: The way is to use length():

int len;
len = Edit1->Text.length();

Q: Which way is correct- “int* p;” or “int *p;”?
A: Both ways are correct. It’s a matter of style (C or C++) which one you use.

A “typical C programmer writes “int *p; and explains it “*p is what is the int” emphasizing syntax, and may point to the C (and C++) declaration grammar to argue for the correctness of the style. Indeed, the * binds to the name p in the grammar.

A “typical C++ programmer writes “int* p; and explains it “p is a pointer to an int” emphasizing type. Indeed the type of p is int*.

Q: How can I declare a generic pointer pointing to a function?
A:

typedef int (*funcptr)();

The above declaration defines a typedef, funcptr, for a pointer to a function (taking unspecified arguments and returning an int). You can now declare variables as

funcptr pf1,pf2;

The above is equivalent to the comples declaration

int (*pf1)(), (*pf2)();

Added by: Saradhi

Q: Is int *p = 4 valid? What does it mean? ie p = 4 or *p = 4?
A: Yes, int *p is perfectly valid. The above declaration declares a variable p of pointer to an integer type and points to the integer present at the memory location 4. The problem arises when you try to write to that location as

*p = 10;

The program’s behavior will be unexpected as p may point to sensitive memory locations like that of the operating system or the program itself. The program may hang or crash.

Added by: Saradhi

Q: How can I access two bytes i.e a short value using a char * pointer?
A: By judicial use of the typecast one can access any data type with a char * pointer. (In general with any pointer variable of any type).

char *t;

t = (char *) malloc(2*sizeof(char));
//Now do some allocations in the memory but do not modify t
printf("%d", *(short *) t);

By this way we can access the two byte short value in a single go.

Q: Are there void pointers?
A: All pointers are pointers to memory. Thats why they are called pointers.
A void pointer is just a pointer with no type information.It can point to anything. You may not dereference it nor may you use pointer arithmetic on it because the compiler doesn’t understand sizeof(void) and who can blame it. You use them really when you need to pass a variety of differently typed pointers to a function.memcpy() takes a void* in its parameter list. This allows you to copy single characters as easily as copying large objects. A from address,a to address and a count of bytes.

Added by: Saradhi

Tags: ,

2 Responses to “Pointers, References, Arrays, Strings”

  1. Klima says:

    How I may finnd more information?

Leave a Reply

You must be logged in to post a comment.