Search code examples
cpointersstring-literals

c - Why do they all point to the same place?


Why do they all point to the same place? It is an optimization?

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <time.h>
#include <conio.h>


const char* func1(const char str[]){
//  printf("%p\n", str);
    return str;
}

const char* func2(const char *str){
//  printf("%p\n", str);
    return str;
}

char* func3(char str[]){
//  printf("%p\n", str);
    return str;
}

char* func4(char *str){
//  printf("%p\n", str);
    return str;
}


int main(void)
{
    char acStr[81] = {0};
    const char *p1, *p2;
    char *p3, *p4;
    uint32_t uiCount;
    srand (time(NULL)); 

    for(uiCount = 0; uiCount < (sizeof(acStr) - 1); uiCount++){
        acStr[uiCount] = rand() % 26 + 65;
    }
    acStr[80] = 0;
    p1 = func1(acStr);
    printf("p1 == %p\n", p1);
    printf("%s", p1);

    for(uiCount = 0; uiCount < (sizeof(acStr) - 1); uiCount++){
        acStr[uiCount] = rand() % 26 + 65;
    }
    acStr[80] = 0;
    p2 = func2(acStr);
    printf("p2 == %p\n", p2);
    printf("%s", p2);

    for(uiCount = 0; uiCount < (sizeof(acStr) - 1); uiCount++){
        acStr[uiCount] = rand() % 26 + 65;
    }
    acStr[80] = 0;
    p3 = func3(acStr);
    printf("p3 == %p\n", p3);
    printf("%s", p3);

    for(uiCount = 0; uiCount < (sizeof(acStr) - 1); uiCount++){
        acStr[uiCount] = rand() % 26 + 65;
    }
    acStr[80] = 0;
    p4 = func4(acStr);
    printf("p4 == %p\n", p4);
    printf("%s", p4);


    printf("\n");


    printf("p1 == %p\n", p1); /* Same address */
    printf("%s", p1);

    printf("p2 == %p\n", p2); /* Same address */
    printf("%s", p2);

    printf("p3 == %p\n", p3); /* Same address */
    printf("%s", p3);

    printf("p4 == %p\n", p4); /* Same address */
    printf("%s", p4);


    getch();
    return 0;
}

Output.

p1 == 000000000022FDC0
USOPBBREKRTCCAXRFVPJPEVPESVTAIQUXIPNMCAWHZGWWUSUUNCWNGFRCTHLJLANVSRQJCTCOOXQZIYX
p2 == 000000000022FDC0
SVITQWBDXTQSUJKXIUKIANTUELJCJPVDYEBCIDGDWITCTZJTDERRPINICWNSIIKMAVTFKIUHEEGNEKBD
p3 == 000000000022FDC0
IDTZXTQWPSRURMWBCAKXWKXJANLVHRDMDREGKBYKJZMDHYHSGRMYAAAGWWRWSAJMBYODZYBKMYPPMVXN
p4 == 000000000022FDC0
TFCBRYSYRNNDUEEOQAPMAGOVQKYNIKNGOQJSBFKYTVRIGWHZMCLFNASRRSGDZWIXOVKYBRYPQOEXRFUJ

p1 == 000000000022FDC0
TFCBRYSYRNNDUEEOQAPMAGOVQKYNIKNGOQJSBFKYTVRIGWHZMCLFNASRRSGDZWIXOVKYBRYPQOEXRFUJ
p2 == 000000000022FDC0
TFCBRYSYRNNDUEEOQAPMAGOVQKYNIKNGOQJSBFKYTVRIGWHZMCLFNASRRSGDZWIXOVKYBRYPQOEXRFUJ
p3 == 000000000022FDC0
TFCBRYSYRNNDUEEOQAPMAGOVQKYNIKNGOQJSBFKYTVRIGWHZMCLFNASRRSGDZWIXOVKYBRYPQOEXRFUJ
p4 == 000000000022FDC0
TFCBRYSYRNNDUEEOQAPMAGOVQKYNIKNGOQJSBFKYTVRIGWHZMCLFNASRRSGDZWIXOVKYBRYPQOEXRFUJ

I'm not being clear, it's because I do not speak English (Translated by Google translante)


Solution

  • In your 4 functions you return the same pointer that was passed as an argument, thus it doesn't change. It's still pointing to the same acStr variable.

    Adding const to the argument type doesn't create a copy; it just prevents you from changing its content.

    Also str[] and *str are the same in this case.