c++ - Why use pointers? -


i know basic question, i've started basic c++ programming after coding few projects high-level languages.

basically have 3 questions:

  1. why use pointers on normal variables?
  2. when , should use pointers?
  3. how use pointers arrays?

  • why use pointers on normal variables?

short answer is: don't. ;-) pointers used can't use else. either because lack of appropriate functionality, missing data types or pure perfomance. more below...

  • when , should use pointers?

short answer here is: cannot use else. in c don't have support complex datatypes such string. there no way of passing variable "by reference" function. that's have use pointers. can have them point @ virtually anything, linked lists, members of structs , on. let's not go here.

  • how use pointers arrays?

with little effort , confusion. ;-) if talk simple data types such int , char there little difference between array , pointer. these declarations similar (but not same - e.g., sizeof return different values):

char* = "hello"; char a[] = "hello"; 

you can reach element in array this

printf("second char is: %c", a[1]); 

index 1 since array starts element 0. :-)

or equally this

printf("second char is: %c", *(a+1)); 

the pointer operator (the *) needed since telling printf want print character. without *, character representation of memory address printed. using character instead. if had used %s instead of %c, have asked printf print content of memory address pointed 'a' plus 1 (in example above), , wouldn't have had put * in front:

printf("second char is: %s", (a+1)); /* wrong */ 

but not have printed second character, instead characters in next memory addresses, until null character (\0) found. , things start dangerous. if accidentally try , print variable of type integer instead of char pointer %s formatter?

char* = "hello"; int b = 120; printf("second char is: %s", b); 

this print whatever found on memory address 120 , go on printing until null character found. wrong , illegal perform printf statement, work anyway, since pointer of type int in many environments. imagine problems might cause if use sprintf() instead , assign way long "char array" variable, got limited space allocated. end writing on else in memory , cause program crash (if lucky).

oh, , if don't assign string value char array / pointer when declare it, must allocate sufficient amount of memory before giving value. using malloc, calloc or similar. since declared 1 element in array / 1 single memory address point at. here's few examples:

char* x; /* allocate 6 bytes of memory me , point x first of them. */ x = (char*) malloc(6); x[0] = 'h'; x[1] = 'e'; x[2] = 'l'; x[3] = 'l'; x[4] = 'o'; x[5] = '\0'; printf("string \"%s\" @ address: %d\n", x, x); /* delete allocation (reservation) of memory. */ /* char pointer x still pointing address in memory though! */ free(x); /* same malloc here allocated space filled null characters!*/ x = (char *) calloc(6, sizeof(x)); x[0] = 'h'; x[1] = 'e'; x[2] = 'l'; x[3] = 'l'; x[4] = 'o'; x[5] = '\0'; printf("string \"%s\" @ address: %d\n", x, x); /* , delete allocation again... */ free(x); /* can set size @ declaration time */ char xx[6]; xx[0] = 'h'; xx[1] = 'e'; xx[2] = 'l'; xx[3] = 'l'; xx[4] = 'o'; xx[5] = '\0'; printf("string \"%s\" @ address: %d\n", xx, xx); 

do note can still use variable x after have performed free() of allocated memory, not know in there. notice 2 printf() might give different addresses, since there no guarantee second allocation of memory performed in same space first one.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -