C: how to assign char * to const char * const *

2

A function returns a certain number of strings ( uint_32 extensions_count ) and I have to assign each of them in const char* const* extensions . The chains are in this structure:

typedef struct VkExtensionProperties {
    char        extensionName[VK_MAX_EXTENSION_NAME_SIZE];
    uint32_t    specVersion;
} VkExtensionProperties;

the value of extensions_count varies around 4 (previously obtained) and the chains are stored in available_extensions

VkExtensionProperties * available_extensions=(VkExtensionProperties*)malloc(sizeof(VkExtensionProperties) * extensions_count );
vkEnumerateInstanceExtensionProperties(NULL, &extensions_count, available_extensions )

The question is how to assign the chains in available_extensions to extensions (only C).

const char* extensions[extensions_count];

for(size_t i=0;i < extensions_count; i++){
if(strcmp(VK_KHR_PLATFORM_SURFACE_EXTENSION_NAME,available_extensions[i].extensionName) == 0){
        for(size_t j=0;j < extensions_count; j++){
            //Aqui la duda
            //extensions[j] = available_extensions[j].extensionName;
        }
        printf("Extension name found\n%s\n",available_extensions[i].extensionName);
        break;
    }
}

What I normally get is a segment violation and I do not stop there. If possible with strcpy , I really do not know how to use it in this particular case. Thank you in advance for the help.

    
asked by Ulises Martínez Elías 01.08.2017 в 16:18
source

2 answers

3

The copy you have to do if or if with strcpy:

char *strcpy(char *dest, const char *src)

Where dest is the pointer to the target array where the string will be copied and src is the string to be copied.

It's giving you segment violation because you're not allocating memory for strings.

  

const char * extensions [extensions_count];

It is an array of pointers of length extensions_count . You need to allocate memory for each pointer in each position. For example.

extensions[i] = malloc(N * sizeof(char)); 

Where N is the length of your string.

Once you have reserved the memory and you can do the strcpy() .

strcpy(extensions[i], "Tu string");
    
answered by 01.08.2017 в 17:16
0
const char* extensions[extensions_count];

There you are declaring an array of pointers ... but those pointers do not point anywhere ... they are uninitialized.

With this in mind you only have two options:

  • Let these pointers point to the corresponding string. Note that in this case you have two pointers pointing to the same chain (be careful with the modifications):

    for(size_t j=0; j < extensions_count; j++)
      extensions[j] = available_extensions[j].extensionName;
    
  • Initialize the pointers (each point to its own buffer) and copy the strings. In this case you will have two independent copies of each string:

    // opcion 1
    for(size_t j=0; j < extensions_count; j++)
      extensions[j] = strdup(available_extensions[j].extensionName);
    
    
    // opcion 2
    for(size_t j=0; j < extensions_count; j++)
    {
      extensions[j] = malloc(strlen(available_extensions[j].extensionName));
      strcpy(extensions[j],available_extensions[j].extensionName);
    }
    
    // ...
    
    for(size_t j=0; j < extensions_count; j++)
      free(extensions[j]);
    
  • The problem you can find? Miscellaneous:

    • That this seemingly silly element that is const can cause the elements to be stored in a special position of the read-only memory and any attempt at modification leads to an error because the Operating System is killing your process to avoid the corruption of memory. It can also happen that the compiler warns you if you try to modify the constant content. If this is the origin of the problem perhaps the most sensible thing to do then is to remove const :

      char* extensions[extensions_count];
      
    • You should guarantee that extensions_count is greater than zero. The binary in C does not tend to like arrays of length 0.

    On the other hand, you say you get a segment violation ... but not where it's happening. Perhaps the true source of the problem is in another part of the code (although it is manifested in this section of the code).

    As a bonus, I leave a small guide to understand the meaning of const depending on where you are:

    • char* : Default pointer. Nothing is constant and you can play freely with both the pointer and the targeted memory.

    • const char* : a non-constant pointer points to a constant string. The pointer can point to where you want but you should not try to modify its content.

    • char* const : The pointer is constant . You should not try to change the direction it points to. The content noted if it can be modified.

    • const char* const : Everything is constant , both the pointer and the targeted memory.

    answered by 02.08.2017 в 02:46