0

I am new to C and not sure how to pass pointers/addresses properly when it comes to chars/strings. I can't get my head around these "strings", about how to master the pointers.

I basically want to pass the address of the array "a" defined below to an arbitrary function, but I don't know how to define the pointer for this array.

Please help me!

I have the following code:

void change(char** a){
    a[0][0]='k';    //that should change a inside main
}

void main() {
    char a[2][3];
    char *tempWord;
    tempWord="sa";
    a[0]=tempWord;
    a[1]=tempWord;
    change(&a);
}
Hogstrom
  • 3,581
  • 2
  • 9
  • 25
Valentin
  • 13
  • 6
  • 1
    Are you asking about [`char **`](https://stackoverflow.com/q/604099/2970947)? – Elliott Frisch Apr 16 '19 at 00:49
  • @ElliottFrisch I can't understand that properly either, I ve heard that is the same as an array of arrays of chars. I just want to modify the array inside another function bassically. – Valentin Apr 16 '19 at 00:51
  • *I ve heard that is the same as an array of arrays of chars.* And what do you think a `char a[2][3];` is? You should show your function, describe your desired behavior and show us an attempt at **that**. – Elliott Frisch Apr 16 '19 at 00:54
  • @ElliottFrisch I hope that s better, I m trying to pass the address of that array of "strings". – Valentin Apr 16 '19 at 01:00

1 Answers1

-1

You have a char[2][3] so you can pass a char (*)[2][3] to your change function. To copy tempWord into your char[][] you can use strncpy. Assuming I understand what you're trying to do, that might look like

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

void change(char (*a)[2][3]) {
    *a[0][0] = 'k';
}

int main() {
    char a[2][3];
    char *tempWord = "sa";
    strncpy(a[0], tempWord, strlen(tempWord));
    strncpy(a[1], tempWord, strlen(tempWord));
    change(&a);
}

Is there any other definition of pointer other than char(*a)[2][3]?

I guess you really did want a char **; you will need to malloc and free your memory for that. Something like

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

void change(char **a) {
    a[0][0]='k';
}

int main() {
    char **a;
    char *tempWord = "sa";
    a = malloc(2 * sizeof(char **));
    a[0] = malloc(strlen(tempWord) * sizeof(char *));
    a[1] = malloc(strlen(tempWord) * sizeof(char *));
    strncpy(a[0], tempWord, strlen(tempWord));
    strncpy(a[1], tempWord, strlen(tempWord));
    change(a);
    printf("%s\n", a[0]);
    free(a[1]);
    free(a[0]);
    free(a);
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • Is there any other definition of pointer other than char(*a)[2][3]? Because I would like to pass the array address without knowing its sizes.@ElliottFrisch – Valentin Apr 16 '19 at 01:18
  • Another question regarding free function. If we only free a[0] and a[1] do we have to free "a" as well as "a" just holds the pointers a[0] and a[1] ?@ElliottFrisch Thank you! – Valentin Apr 16 '19 at 01:47
  • If you `malloc` three times, you need to `free` three times. – Elliott Frisch Apr 16 '19 at 02:05
  • Should it not be a[0] = malloc((strlen(tempWord)+1) * sizeof(char *));? as you allocate memory for that '\0'? @ElliottFrisch – Valentin Apr 16 '19 at 02:12
  • In the first example it should be `(*a)[0][0] = k`; . In this exact case (all indices are 0) it doesn't matter , but it would matter if they used any other indices. – M.M Apr 16 '19 at 05:17
  • The second program causes undefined behaviour, `printf("%s\n"` should only have a null-terminated string as argument and `a[0]` is not null-terminated. Also all of the malloc calls request the wrong size. (I'd recommend standard pattern `p = malloc( N * sizeof *p );` – M.M Apr 16 '19 at 05:19
  • @M.M, you said "p = malloc( N * sizeof *p );", what is the type of p ? – Valentin Apr 17 '19 at 22:50
  • @BurLeXyOOnuTz it doesn't matter, which is why this form is good – M.M Apr 18 '19 at 08:44
  • @M.M, sorry I meant does p have to be pointer of a pointer, or literally anything? Because if char* p=malloc(3* sizeof (char)) would sound fair? – Valentin Apr 18 '19 at 13:00