Pass char pointer/array to function


open

I'm trying to understand more about char pointers in C, but one thing got me.

Suppose I want to pass a char pointer to a function and change the value represented by that pointer. Here is an example:

int Foo (char *(&Msg1), char* Msg2, char* Msg3){
    char *MsgT = (char*)malloc(sizeof(char)*60);
    strcpy(MsgT,"Foo - TEST");
    Msg1 = MsgT; // Copy address to pointer
    strcpy(Msg2,MsgT); // Copy string to char array
    strcpy(Msg3,MsgT); // Copy string to char pointer
    return 0;
}

int main() {
    char* Msg1; // Initial char pointer
    char Msg2[10]; // Initial char array
    char* Msg3 = (char*)malloc(sizeof(char) * 10); // Preallocate pointer memory
    Foo(Msg1, Msg2, Msg3);
    printf("Msg1: %s\n",Msg1); // Method 1
    printf("Msg2: %s\n",Msg2); // Method 2
    printf("Msg3: %s\n",Msg3); // Method 3
    free(Msg1);    
    free(Msg3);
    return 0;
}

In the example above, I've listed all the working methods I know of to pass a char pointer to a function. What I don't understand is method 1 .

char *(&Msg1)What is the meaning of the first parameter passed to the function ?Foo

Also, method 2 and method 3 seem to be covered extensively by books and tutorials, some of which even refer to those methods as the most correct way to pass arrays/pointers . I don't know that method 1 looks fine to me, especially when I write an API where users can easily pass null pointers to functions without pre-allocating memory. If the user forgets to free the memory block ( same as method 3 ) , the only downside could be a potential memory leak . Is there any reason why we would prefer to use method 2 or 3 over method 3 ?

David K

int f(char* p)is the usual way in C to pass a pointer to the function when it already points to the storage location you need (usually because a character array has been allocated in either method 2 or 3) .pfp

int f(char** p)is the usual way in C to pass a pointer to a function when you want to be able to modify the pointer for the caller of the function . Your method 1 is an example. You want to allocate new memory and use it to tell the caller where that memory is.pffpfp

int f(char*& p)is C++, not C. Since this is compiled for you, we know you are using a C++ compiler.

Related


Pass char value to function

username I have the following function: CREATE OR REPLACE FUNCTION BANINST1."F_COC_AUTO_AWARD_FILTER" (pidm number) return number as return_field number; cursor get_pidm is select distinct SHRDGMR.SHRDGMR_PIDM from SATURN.SHRDGMR SHRDGMR, SATURN.SORLCUR

Pass char array to function

under the moonlight I have to call a method with the following signature: int sendTo(const void* buffer, int length, const SocketAddress& address, int flags=0); My first question is: What exactly const void* bufferdoes that mean? My intention: it means it's a

How to pass char to function?

AsiandudeCom I want to randomize my string so here is my code. while(strcmp(word,"END")!=0) { printf("Enter word"); fgets(input,sizeof(input),stdin); sscanf(input,"VERTEX %s",key1); strcpy(list[count],key1); count++; } random(list); I declare list and key1 as

Pass char value to function

username I have the following function: CREATE OR REPLACE FUNCTION BANINST1."F_COC_AUTO_AWARD_FILTER" (pidm number) return number as return_field number; cursor get_pidm is select distinct SHRDGMR.SHRDGMR_PIDM from SATURN.SHRDGMR SHRDGMR, SATURN.SORLCUR

Pass char array to function

under the moonlight I have to call a method with the following signature: int sendTo(const void* buffer, int length, const SocketAddress& address, int flags=0); My first question is: What exactly const void* bufferdoes that mean? My intention: it means it's a

How to pass char to function?

AsiandudeCom I want to randomize my string so here is my code. while(strcmp(word,"END")!=0) { printf("Enter word"); fgets(input,sizeof(input),stdin); sscanf(input,"VERTEX %s",key1); strcpy(list[count],key1); count++; } random(list); I declare list and key1 as

Pass a char array to a function that takes a char pointer

Bruno Ely In C, why can I pass a character array to a function that takes a char *as a parameter, but not the address of the array to a function that takes a char **? UPDATE: Interestingly, changing the parameter type char* qux[12]to does n't completely change

Pass a char array to a function that takes a char pointer

Bruno Ely In C, why can I pass a character array to a function that takes a char *as a parameter, but not the address of the array to a function that takes a char **? UPDATE: Interestingly, changing the parameter type char* qux[12]to does n't completely change

Pass a char array to a function that takes a char pointer

Bruno Ely In C, why can I pass a character array to a function that takes a char *as a parameter, but not the address of the array to a function that takes a char **? UPDATE: Interestingly, changing the parameter type char* qux[12]to does n't completely change

Pass an array of char pointers to a function

Andrew Hummel I am trying to pass an initialized array of char pointers to a function. I can't seem to figure out why the function just prints out the number for each element in the array. Does anyone know how I can print each string element from the passed in

Pass char pointer to C function

tiktak I am trying to get the mac address with the following code: void getMacAdress(unsigned char **address) { int s; struct ifreq buffer; s = socket(PF_INET, SOCK_DGRAM, 0); memset(&buffer, 0x00, sizeof(buffer)); strcpy(buffer.ifr_name,

Pass char *str inside function

Alpha I just started , and I felt comfortable C++writing Javait for a while . So, I have this array, char values[][10] = {"miami", "seattle", "berlin"}; int rows = sizeof values / sizeof values[0]; This is the function where I want to pass the value, // a fu

Pass an array of char pointers to a function

Andrew Hummel I am trying to pass an initialized array of char pointers to a function. I can't seem to figure out why the function just prints out the number for each element in the array. Does anyone know how I can print each string element from the passed in

Pass an array of char pointers to a function

Andrew Hummel I am trying to pass an initialized array of char pointers to a function. I can't seem to figure out why the function just prints out the number for each element in the array. Does anyone know how I can print each string element from the passed in

Pass an array of char pointers to a function

Andrew Hummel I am trying to pass an initialized array of char pointers to a function. I can't seem to figure out why the function just prints out the number for each element in the array. Does anyone know how I can print each string element from the passed in

Pass char array to another function

saswat23 I can't pass char array from function to main. Instead of the actual array, it shows some unwanted symbols. please help me. #include <stdio.h> char* setDestPath (int x, char inp_path[x]) { int ret, cnt=0, i=0, j, temp=0; char dest_path[x], out_

Pass buffer (char*) to function in C

Roger Tannous I am passing a buffer (char*) to a function in C. Inside the function, I'm allocating memory for the buffer and appending a string (response from the virtual server). When printed inside the function, the string will appear as the string sent fro

Pass const char* to function in C

Dan Brenner I'm trying to write a function that takes a const char *as a parameter, but I don't know how to pass such data to the function in a useful way. If I have the function: void tokenize(const char * c) { } I want to call this function with a hardcoded

Cannot pass char array to function

Oz123 I'm having a hard time passing charan array to a function: Here is the code found inside a function that calls another function createbitshape: char ans[8]; char bitshp[8]; ... fgets(ans, 10, stdin); createbitshape(random_num, bitshp); printf("bitshp out

Pass const char* to function in C

Dan Brenner I'm trying to write a function that takes a const char *as a parameter, but I don't know how to pass such data to the function in a useful way. If I have the function: void tokenize(const char * c) { } I want to call this function with a hardcoded

Pass an array of char arrays to a function

Rabida In my program I may need to load a large file, but not always. So I defined: char** largefilecontents; string fileName="large.txt"; When I need to load a file, the program calls this function: bool isitok=LoadLargeFile(fileName,largefilecontents); The

Pass an array of char pointers to a function

Sorabis I have written the following sample code to demonstrate my problem #include <iostream> #include <string.h> using namespace std; void f (char*** a) { *a = new (char*[2]); *a[0] = new char[10]; *a[1] = new char[10]; strcpy(*a[0], "abcd"

Pass pointer to char array to function

bob jane Below is my function that takes a char array of size 2, passes it to the function, and the function should return the same 2 chars (it's a bit complicated when it comes to communicating with hardware devices). The problem is when I pass char(*in)[2] t

Pass an array of char arrays to a function

Rabida In my program I may need to load a large file, but not always. So I defined: char** largefilecontents; string fileName="large.txt"; When I need to load a file, the program calls this function: bool isitok=LoadLargeFile(fileName,largefilecontents); The

Pass char pointer as parameter to function

unlimited This item table prints different permutations of strings. If I declare the string as a char array in main and pass the array name in the printAnagram function it works fine. But if I declare string as char *s="hello" and pass 's' it crashes. Why? #in

Pass char pointer to C function

tiktak I am trying to get the mac address with the following code: void getMacAdress(unsigned char **address) { int s; struct ifreq buffer; s = socket(PF_INET, SOCK_DGRAM, 0); memset(&buffer, 0x00, sizeof(buffer)); strcpy(buffer.ifr_name,

Pass an array of char pointers to a function

Andrew Hummel I am trying to pass an initialized array of char pointers to a function. I can't seem to figure out why the function just prints out the number for each element in the array. Does anyone know how I can print each string element from the passed in

Pass an array of char pointers to a function

Andrew Hummel I am trying to pass an initialized array of char pointers to a function. I can't seem to figure out why the function just prints out the number for each element in the array. Does anyone know how I can print each string element from the passed in

Pass char array to another function

saswat23 I can't pass char array from function to main. Instead of the actual array, it shows some unwanted symbols. Please help me. #include <stdio.h> char* setDestPath (int x, char inp_path[x]) { int ret, cnt=0, i=0, j, temp=0; char dest_path[x], out_