How do C’s hands work?

Asked

Viewed 925 times

10

first What is the difference between declaring: char* s and char *s?

2nd It is always necessary to use the function malloc when declaring a inter?

Example:

 char* s = malloc(sizeof(char));

 //ou apenas...

 char* s;

Declare a pointer char* s it’s like I’m declaring a string? I can use it as a String?

fourth Example:

char* s;
char s;

What’s the difference between them?

5th What is the real use of ponteiros? In addition to solving some bugs?

2 answers

11

The question is too broad, it should be separated, but come on:

What is the difference between declaring: char* s and char *s?

None, is it taste.

It is always necessary to use the function malloc when declaring a inter?

No, but whenever you declare a pointer, it’s likely that at some point you’ll have to put a value there. Most often it will be directly or indirectly through the malloc(), emphasis on the indirect. But it is possible to point to other areas of memory, such as the stack (usually by creating a array or structure, but in theory any address can be used by using the operator & to pick up the address) or static area, including where the methods are (pointer to function). The malloc() is used to allocate memory in heap.

Declare a pointer char* s it’s like I’m declaring a string? I can use it as a string?

Roughly speaking we can say yes, but C doesn’t have a type string native, not exactly the same thing.

Example: char* s; and char s; What’s the difference between them?

The first is a pointer to a character, even there may be a sequence of them from that point in memory, the programmer must control this. The second is just a character.

What is the real usefulness of pointers? Besides solving some bugs?

Pointers do not solve bugs. This is insane. Pointer is an address to some point in memory, that’s it. It is used to indicate where certain objects are. Having access to memory indirectly. As the name says, it serves to point to things elsewhere.

It can be better answered in several questions here, so I did not give more details:

  • About item 4, very well described. Generally people tend not to explain the difference correctly.

11


What is the difference between declaring: char* s and char *s?

None. Aesthetics, only. It’s less confusing to write:

char *s, *r than char* s, *r, p.and.

2º It is always necessary to use the malloc function whenever declaring a Pointer?

No, you’re confusing things. A pointer is nothing more than a variable that stores an address. In the same way as an integer aramazena a number. As the various types (int, char, etc.) have different sizes, there is a different pointer for each type. This occurs because of arithmetic with pointers, which I explain later.

char c = 'a';
char *s = &a;

The above code declares a variable c which stores a character and a variable s which stores the address of c. We could still create a variable for store the address of s:

char **ss = &s;

Staying:

Variável    Endereço (hipotético)     Valor     Tamanho
c           0x0002                    'a'       sizeof(char) bytes
s           0x0010                    0x0002    sizeof(char *) bytes
ss          0xFF10                    0x0010    sizeof(char **) bytes

malloc is a function that reserves a continuous memory space on the heap, returning the address of the beginning of this space (pointer type, as it is a address). P.e.

char *s = malloc(2);

Reserve two bytes, and the initial address is assigned to the variable s:

Variável    Endereço (hipotético)     Valor     Tamanho
s           0x0001                    ?         2 bytes

If we print:

printf("%p\n", s + 1);

We’ll see 0x0002, s address one more. That’s because the size of char is 1 byte (s + 1 translates to sizeof(*s) + 1). For this arithmetic question (used in arrays, among other things) is that each type has a "pointer type" equivalent including pointer hands of ...

3º Declaring a char* s variable is like declaring a string? Can I use this as a String?

Not. char *s contains an address. Dot. A string is a character array, which can be declared in several ways (search for when to use malloc, or ask a new question, and read the manual on the use of malloc):

char *s = malloc(10 * sizeof(*s));
char s2[10];
strncpy(s, "123456789", 10);
strncpy(s2, "123456789", 10);
char s3[] = "123456789";
char *s4 = strdup("123456789");
free(s);
free(s4);

Read the manual of the above functions if you want to understand more about them. In Linux/OSX: man FUNCAO.

char* s; char s;

As already explained, one guard a character and the other an address.

5th What is the real use of pointers? Besides solving some bugs?

Perhaps this answer gave an initial idea about pointers. They are used for absolutely everything, as you will see when learning more.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.