Jump to content

How to make a array of pointers?

duckwithanokhat

In C, how do I make an array of pointers that point to multiple strings. For example:

 

char input[10];

char input2[10];

 

And then an array where the first element points to input and the second one points to input2.

 

Link to comment
Share on other sites

Link to post
Share on other sites

On phone so no code tags:

char* stringArray[2];

stringArray[0] = input;

stringArray[1] = input2;

 

And since Im a big advocate of C++:

std::string stringArray[2];

Desktop: Intel i9-10850K (R9 3900X died 😢 )| MSI Z490 Tomahawk | RTX 2080 (borrowed from work) - MSI GTX 1080 | 64GB 3600MHz CL16 memory | Corsair H100i (NF-F12 fans) | Samsung 970 EVO 512GB | Intel 665p 2TB | Samsung 830 256GB| 3TB HDD | Corsair 450D | Corsair RM550x | MG279Q

Laptop: Surface Pro 7 (i5, 16GB RAM, 256GB SSD)

Console: PlayStation 4 Pro

Link to comment
Share on other sites

Link to post
Share on other sites

48 minutes ago, mathijs727 said:

On phone so no code tags:

char* stringArray[2];

stringArray[0] = input;

stringArray[1] = input2;

 

And since Im a big advocate of C++:

std::string stringArray[2];

How would this assign the address of input and input2 to the array?

Link to comment
Share on other sites

Link to post
Share on other sites

7 minutes ago, duckwithanokhat said:

How would this assign the address of input and input2 to the array?

Those are the final 2 lines:

stringArray[0] = input;

 

stringArray is an array of char pointers

Desktop: Intel i9-10850K (R9 3900X died 😢 )| MSI Z490 Tomahawk | RTX 2080 (borrowed from work) - MSI GTX 1080 | 64GB 3600MHz CL16 memory | Corsair H100i (NF-F12 fans) | Samsung 970 EVO 512GB | Intel 665p 2TB | Samsung 830 256GB| 3TB HDD | Corsair 450D | Corsair RM550x | MG279Q

Laptop: Surface Pro 7 (i5, 16GB RAM, 256GB SSD)

Console: PlayStation 4 Pro

Link to comment
Share on other sites

Link to post
Share on other sites

24 minutes ago, mathijs727 said:

Those are the final 2 lines:

stringArray[0] = input;

 

stringArray is an array of char pointers

So you don't need the &?

Link to comment
Share on other sites

Link to post
Share on other sites

22 minutes ago, duckwithanokhat said:

So you don't need the &?

No, because input and input2 are already char pointers.

An array in C is the same as a pointer.

 

A static array in C allocates memory on the stack and the variable is a pointer, pointing to that memory.

Desktop: Intel i9-10850K (R9 3900X died 😢 )| MSI Z490 Tomahawk | RTX 2080 (borrowed from work) - MSI GTX 1080 | 64GB 3600MHz CL16 memory | Corsair H100i (NF-F12 fans) | Samsung 970 EVO 512GB | Intel 665p 2TB | Samsung 830 256GB| 3TB HDD | Corsair 450D | Corsair RM550x | MG279Q

Laptop: Surface Pro 7 (i5, 16GB RAM, 256GB SSD)

Console: PlayStation 4 Pro

Link to comment
Share on other sites

Link to post
Share on other sites

21 minutes ago, mathijs727 said:

No, because input and input2 are already char pointers.

An array in C is the same as a pointer.

 

A static array in C allocates memory on the stack and the variable is a pointer, pointing to that memory.

I once saw a Pros and Cons list of using C, and "pointers" was in both columns. xD

Make sure to quote or tag me (@JoostinOnline) or I won't see your response!

PSU Tier List  |  The Real Reason Delidding Improves Temperatures"2K" does not mean 2560×1440 

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, mathijs727 said:

On phone so no code tags:

char* stringArray[2];

stringArray[0] = input;

stringArray[1] = input2;

 

And since Im a big advocate of C++:

std::string stringArray[2];

Or initialize the array directly to remove the explicit array size, which is always better since it allows later changes to the array contents without introducing bugs by forgetting to manually set a new array size.

char* stringArray[] = {input, input2};

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, mathijs727 said:

An array in C is the same as a pointer.

It is not!

 

Array names decay into pointers to the first element of the array, but a array is not a pointer:

char *pointer;
char array[10];
printf("Pointer size %zd is not equal to array size %zd!\n", sizeof(pointer), sizeof(array));

//also...
printf("Pointer arithmetic for decayed pointer: %p\n", array + 1);
printf("Is not the same as pointer arithmetic for the array itself: %p\n", &array + 1);
//Because array decays into a pointer to the first element of type char*
//While &array takes the address of the array itself, of type char(*)[10]

 

Link to comment
Share on other sites

Link to post
Share on other sites

On 2/7/2017 at 5:29 AM, Unimportant said:

It is not!

 

Array names decay into pointers to the first element of the array, but a array is not a pointer:


char *pointer;
char array[10];
printf("Pointer size %zd is not equal to array size %zd!\n", sizeof(pointer), sizeof(array));

//also...
printf("Pointer arithmetic for decayed pointer: %p\n", array + 1);
printf("Is not the same as pointer arithmetic for the array itself: %p\n", &array + 1);
//Because array decays into a pointer to the first element of type char*
//While &array takes the address of the array itself, of type char(*)[10]

 

This is correct, and bound to bite every beginner C and C++ programmer sooner or later, especially when trying to pass arrays to functions. See below:

#include <stdio.h>
 
#define ARRAY_SIZE(x) sizeof(x)/sizeof(x[0])
  
//Deceiving, because (EDIT: pointer/array) arguments to functions are ALWAYS 
//simply pointers, NOT arrays
void arraySize(char charArray[])
{
    printf("ARRAY_SIZE(charArray) = %zu\n", ARRAY_SIZE(charArray)); //Prints 4!
}
  
int main()
{
    char myCharPointer[50]; //Allocates an array of size 50 on the stack
	printf("ARRAY_SIZE(myCharPtr) = %zu\n", ARRAY_SIZE(myCharPointer)); //Prints 50
    arraySize(myCharPointer);
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

13 minutes ago, Pinguinsan said:

This is correct, and bound to bite every beginner C and C++ programmer sooner or later, especially when trying to pass arrays to functions. See below:

Which is why std::array was created.

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

19 hours ago, Pinguinsan said:

This is correct, and bound to bite every beginner C and C++ programmer sooner or later, especially when trying to pass arrays to functions. See below:


#include <stdio.h>
 
#define ARRAY_SIZE(x) sizeof(x)/sizeof(x[0])
  
//Deceiving, because arguments to functions are ALWAYS 
//simply pointers, NOT arrays
void arraySize(char charArray[])
{
    printf("ARRAY_SIZE(charArray) = %zu\n", ARRAY_SIZE(charArray)); //Prints 4!
}
  
int main()
{
    char myCharPointer[50]; //Allocates an array of size 50 on the stack
	printf("ARRAY_SIZE(myCharPtr) = %zu\n", ARRAY_SIZE(myCharPointer)); //Prints 50
    arraySize(myCharPointer);
}

 

You're correct in saying that C passes a pointer for array arguments, but the arguments to functions are never passed by reference; C only passes by value (even pointers! it passes a copy of the address value). See this example code:

void increment(int a) {
  ++a;
}
 
int main(){
    int a = 1;
    printf("before increment = %d\n", a);
  	increment(a);
    printf("after increment = %d\n", a);
}

This will output 

before increment = 1
after increment = 1

When passing any primitive or struct, C creates a copy of the value that it's passing. If instead everything was passed as a pointer, the above code would have incremented the value of the 'a' variable in the main stack frame.

 

The only things that can be considered similar to pass-by-reference (technically it's still by value, since C passes a copy of the pointer to the first array element) are arrays, C does not create a copy of the array. Illustrating this:

void increment(int a[]) {
    ++a[0];
}

int main(){
    int a[1] = {1};
    printf("before increment = %d\n", a[0]);
    increment(a);
    printf("after increment = %d\n", a[0]);
}

Will output

before increment = 1
after increment = 2

 

But C does not just pass pointers, as evident by the primitive not changing in the example (and the same goes for structs), nor does it ever pass by reference. And while this might seem like I'm being pedantic, it's an important distinction. 

 

EDIT: Just to be clear, I'm addressing the "arguments to functions are ALWAYS pointers" part of what you said.

15" MBP TB

AMD 5800X | Gigabyte Aorus Master | EVGA 2060 KO Ultra | Define 7 || Blade Server: Intel 3570k | GD65 | Corsair C70 | 13TB

Link to comment
Share on other sites

Link to post
Share on other sites

4 hours ago, Blade of Grass said:

You're correct in saying that C passes a pointer for array arguments, but the arguments to functions are never passed by reference; C only passes by value (even pointers! it passes a copy of the address value). See this example code:


void increment(int a) {
  ++a;
}
 
int main(){
    int a = 1;
    printf("before increment = %d\n", a);
  	increment(a);
    printf("after increment = %d\n", a);
}

This will output 


before increment = 1
after increment = 1

When passing any primitive or struct, C creates a copy of the value that it's passing. If instead everything was passed as a pointer, the above code would have incremented the value of the 'a' variable in the main stack frame.

 

The only things that can be considered similar to pass-by-reference (technically it's still by value, since C passes a copy of the pointer to the first array element) are arrays, C does not create a copy of the array. Illustrating this:


void increment(int a[]) {
    ++a[0];
}

int main(){
    int a[1] = {1};
    printf("before increment = %d\n", a[0]);
    increment(a);
    printf("after increment = %d\n", a[0]);
}

Will output


before increment = 1
after increment = 2

 

But C does not just pass pointers, as evident by the primitive not changing in the example (and the same goes for structs), nor does it ever pass by reference. And while this might seem like I'm being pedantic, it's an important distinction. 

 

EDIT: Just to be clear, I'm addressing the "arguments to functions are ALWAYS pointers" part of what you said.

Of course when passing a non-pointer type to a function, it will create a local stack copy. My comment was regarding using an "array" as an argument to a function, not for passing regular types.

And yes, while you are technically correct that C does not pass by reference (a function taking a pointer as an argument takes the value of the pointer), everyone knows what you mean when you say it.
"pass by reference".

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×