C passing array of pointers

Silenus

This relates to C. I am having some trouble understanding how I can assign strings to char pointers within arrays from a function.

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

void function(char* array[]);

int main(void)
{
    char* array[50];
    function(array);
    printf("array string 0: %s\n",array[0]);
    printf("array string 1: %s\n",array[1]);

}

void function(char* array[])
{
    char temp[] = "hello";
    array[0] = temp;
    array[1] = temp;

    return;
}

Ideally, I would like the main printf function to return

array string 0: hello
array string 1: hello

But I'm having trouble understanding arrays of pointers, how these pass to functions and how to manipulate them in the function. If I declare a string like char temp[] = "string" then how do I assign this to one of the main function array[i] pointers? (assuming I have my jargon right)

emlai

char temp[] = "hello"; only creates a local, temporary array inside the function. So when the function exists, the array will be destroyed.

But with array[0] = temp; you're making array[0] point to the local array temp.

After the function returns, temp doesn't exist anymore. So accessing array[0] which pointed to temp will cause undefined behavior.


You could simply make temp static, so it also exists outside the function:

static char temp[] = "hello";

Or, you could copy the "hello" string to array[0] and array[1]. For copying C-strings, you normally use strcpy.

char temp[] = "hello";
strcpy(array[0], temp);
strcpy(array[1], temp);

However, before copying you need to make sure array[0] and array[1] point to memory that has enough space to hold all characters of "hello", including the terminating null character. So you have to do something like this before calling strcpy:

array[0] = malloc(6); 
array[1] = malloc(6);

(6 is the minimum numbers of characters that can hold "hello".)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Passing Arrays into an Array of Pointers in C

From Dev

Passing an array of character pointers to a C function

From Dev

C++ passing array of pointers to function

From Dev

Passing "array of pointers" to function

From Dev

Passing An Array of Pointers to Function

From Dev

Passing array of pointers into a function

From Dev

Passing An Array of Pointers to Function

From Dev

Passing array of pointers into a function

From Dev

passing pointers (the name of array) into function in C/C++

From Dev

Passing char pointers in C

From Dev

Passing and changing an array, with pass by reference, using pointers in C

From Dev

2D array passing via pointers in C

From Dev

C++ passing array of pointers of methods to a method as argument

From Dev

C array of pointers to strings, passing string from pointer

From Dev

Passing a pointer to array of string pointers

From Dev

Passing an array of pointers to a Class Object

From Dev

Passing arguments by reference and Pointers in c

From Dev

Passing pointers to function in c++

From Dev

Char passing in C++ with pointers

From Dev

passing pointers to a strcpy function in c

From Dev

C++ Error 1 error C2664 passing array pointers

From Dev

Go: Passing pointers of an array to gob without copying?

From Dev

Store array of pointers in array of pointers in C

From Dev

Calling a C function from Julia and passing a 2D array as a pointer of pointers as argument

From Dev

C - pointer to array of pointers

From Dev

C array of function pointers

From Dev

Pointers to struct and array in C

From Dev

C -- Array of Function Pointers

From Dev

Pointer array of pointers with C?

Related Related

HotTag

Archive