-1

I have just started learning c and finding it pretty hard to wrap my head around pointer concept.

I have came across this code samples.

What does below statement mean? Char *s_item[20];

I know that char *s_item means a character pointer and you need to allocated memory to it before you can assign or copy a string into it. It just stores the beginning address of the string.

Next, what does this statement mean. s_item=(char(*)20)calloc (30,20);

And lastly can someone explain to me the concept of char **values I know that it's a pointer to a pointer. But what kind of values we can store in it exactly. Is it a collection of strings?

redsoxlost
  • 1,215
  • 5
  • 19
  • 32

2 Answers2

3

If you learn about the spiral/clockwise rule you could decipher it as an array of 20 pointers to char. I.e. an array of strings.

Depending on where the definition it, each element will either be null pointers (if it's a global variable) or each element will be uninitialized and have an indeterminate value (if s_item is a local variable). Either way, you must make each pointer in the array point to some valid data before you can really use them, either by making them point to existing strings or by allocating memory dynamically which you then initialize.


As for a pointer to a pointer, it's just the same as any other pointer: It's a pointer that points to something, in this case it's pointing to another pointer.

Example:

char *p = malloc(12);  // Allocate 12 bytes and make the variable p point to it
strcpy(p, "hello world");  // Initialize the memory we just allocated with a string

char **pp = &p;  // Make the variable pp a pointer, and make it point to the variable p

In memory it looks something like this

+----+     +---+     +---------------+
| pp | --> | p | --> | "hello world" |
+----+     +---+     +---------------+

That is, pp is pointing to the variable p, and p is pointing to memory containing the string "hello world".

The example shown above, having pp is pretty much useless, but with pointers to pointers you can emulate pass by reference with function arguments, and for example do something like this:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void function(char **pp)
{
    *pp = malloc(12);  // Allocate memory
    strcpy(*pp, "hello world");
}

int main(void)
{
    char *p;  // Define variable, but don't initialize it

    function(&p);  // Pass a pointer to the variable p

    printf("p = \"%s\"\n", p);  // Will print p = "hello world"
}

By passing the pointer to the variable p (using &p) to the function, the function can modify the pointer (i.e. the value of p).

Another use for pointer to pointer is dynamic array of arrays. For example

char **pp = malloc(5 * sizeof(char *));  // Allocate space for five pointers to char
for (size_t i = 0; i < 5; ++i)
{
    pp[i] = malloc(12);  // Allocate space for a string
    strcpy(pp[i], "hello world");  // Initialize the memory
}

Now we have a dynamically allocated array of five dynamically allocated arrays of 12 characters.

Both uses of pointers to pointers works with other data-types than char of course.

It should be noted that even though arrays naturally decays to a pointer to its first element, an array of arrays (like e.g. char x[20][20]) is not the same as a pointer to pointer (e.g. char **). For an explanation of this see e.g. this old answer of mine.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0
char *s_item[20];

The statement above means "s_item" is an array and its elements are pointers to characters.

s_item=(char(*)20)calloc (30,20);

Something seems wrong with your code. First, (char(*)20) is the forced casting but I think it should be (char*). Second, this is an invalid assignment because you want to assign a char* to a char**.

"calloc" is a C function for dynamic memory allocation. The function returns a (void*), which means a pointer to unknown. And the pointer is cast into a pointer to char*. But it cannot be assigned to s_item because s_item is a pointer to pointer to character.

As for the third question, if the type of a variablepp is char**, we can store an address in pp, which means pp is a pointer. And the address stored in pp must be the address of another pointer which points to a character. So we call pp is the pointer to pointer to character.

F.ZYoe
  • 11
  • 4