Skip to main content
Addressed "t is not a pointer" part of the question
Source Link
paulsm4
  • 122.8k
  • 23
  • 175
  • 245

YouPROBLEM 1:

If you want to create or modify an *int array inside of a function, then you need to pass a "pointer to a pointer":

// WRONG:
void array_push(int *array_pointer, int array_length, int val) {
...
    int *temp_array = malloc(sizeof(int) * (array_length + 1));
...
    *array_pointer = temp_array;

Instead:

// BETTER:
void array_push(int **array_pointer, int array_length, int val) {
...
    int *temp_array = malloc(sizeof(int) * (array_length + 1));
...
    *array_pointer = temp_array;

Or:

// BETTER YET:
int * array_push(int array_length, int val) {
...
    int *temp_array = malloc(sizeof(int) * (array_length + 1));
...
return temp_array;

PROBLEM 2:

If you want to declare a static array like this int t[2] = {0,2};, then you can't arbitrarily change it's size. Here's a good description of "arrays vs pointers":

http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1069897882&id=1073086407

One of the first things a new student learns when studying C and C++ is that pointers and arrays are equivalent. This couldn't be further from the truth...

You need to pass a "pointer to a pointer":

// WRONG:
void array_push(int *array_pointer, int array_length, int val) {
...
    int *temp_array = malloc(sizeof(int) * (array_length + 1));
...
    *array_pointer = temp_array;

Instead:

// BETTER:
void array_push(int **array_pointer, int array_length, int val) {
...
    int *temp_array = malloc(sizeof(int) * (array_length + 1));
...
    *array_pointer = temp_array;

Or:

// BETTER YET:
int * array_push(int array_length, int val) {
...
    int *temp_array = malloc(sizeof(int) * (array_length + 1));
...
return temp_array;

PROBLEM 1:

If you want to create or modify an *int array inside of a function, then you need to pass a "pointer to a pointer":

// WRONG:
void array_push(int *array_pointer, int array_length, int val) {
...
    int *temp_array = malloc(sizeof(int) * (array_length + 1));
...
    *array_pointer = temp_array;

Instead:

// BETTER:
void array_push(int **array_pointer, int array_length, int val) {
...
    int *temp_array = malloc(sizeof(int) * (array_length + 1));
...
    *array_pointer = temp_array;

Or:

// BETTER YET:
int * array_push(int array_length, int val) {
...
    int *temp_array = malloc(sizeof(int) * (array_length + 1));
...
return temp_array;

PROBLEM 2:

If you want to declare a static array like this int t[2] = {0,2};, then you can't arbitrarily change it's size. Here's a good description of "arrays vs pointers":

http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1069897882&id=1073086407

One of the first things a new student learns when studying C and C++ is that pointers and arrays are equivalent. This couldn't be further from the truth...

Source Link
paulsm4
  • 122.8k
  • 23
  • 175
  • 245

You need to pass a "pointer to a pointer":

// WRONG:
void array_push(int *array_pointer, int array_length, int val) {
...
    int *temp_array = malloc(sizeof(int) * (array_length + 1));
...
    *array_pointer = temp_array;

Instead:

// BETTER:
void array_push(int **array_pointer, int array_length, int val) {
...
    int *temp_array = malloc(sizeof(int) * (array_length + 1));
...
    *array_pointer = temp_array;

Or:

// BETTER YET:
int * array_push(int array_length, int val) {
...
    int *temp_array = malloc(sizeof(int) * (array_length + 1));
...
return temp_array;