#Problem trying to set Strings values

22 messages · Page 1 of 1 (latest)

spare wyvern
#

I'm trying to convert a Linked List to a Array, to this i'm creating a Array of Char* with the same size of the Linked List, then i loop through the nodes in the Linked List, allocate the new value, then i copy all the chars, and for some reason, when i run, I always gets the last bit has a random ram value, i did everything i could think to resolve this, use memcpy? Not work, change the size of the new char to sup->value->len - x? Just results in X more values to be random ram values. The only "ok" result i got is when i set lines[count][i + 1] (the last value +1) to something, and i get the value instead of something random.

char **toArray(LinkedList *lista) {
    printf("toArray\n");
    // String *lines = (String *)malloc(sizeof(String) * lista->len);
    char **lines = malloc(sizeof(char *) * lista->len);
    
    Node *sup = lista->start;
    int count = 0;
    while (sup != NULL) {
        lines[count] = (char *)malloc(sizeof(char) * sup->value->len);
        for (int i = 0; i < sup->value->len; i++) 
            lines[count][i] = sup->value->str[i];
        
        // memcpy(lines[count], sup->value->str, sup->value->len);
        printf("%s\n", lines[count]);
        count += 1;
        sup = sup->next;
    }

    return lines;
}

If you want to try here is the code, just run the cmake then ./tager Test\ Files/test1.txt
https://github.com/MarceloLuisDantas/Tager-Assembler

GitHub

Contribute to MarceloLuisDantas/Tager-Assembler development by creating an account on GitHub.

drowsy girderBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

karmic widget
#

i'd use strncpy() since you know the length of your strings

carmine lance
#

oh nvm

spare wyvern
#

nvm?

spare wyvern
#

thanks

#

!solve

drowsy girderBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity

karmic widget
#

!man 3 string

drowsy girderBOT
#

stpcpy, strcasecmp, strcat, strchr, strcmp, strcoll, strcpy, strcspn, strdup, strfry, strlen, strncat, strncmp, strncpy,
strncasecmp, strpbrk, strrchr, strsep, strspn, strstr, strtok,
strxfrm, index, rindex - string operations

Synopsis
#include <strings.h>

int strcasecmp(const char *s1, const char *s2);
       Compare the strings s1 and s2 ignoring case.

int strncasecmp(const char *s1, const char *s2, size_t n);
       Compare the first n bytes of the strings s1 and s2
       ignoring case.

char *index(const char *s, int c);
       Return a pointer to the first occurrence of the character
       c in the string s.

char *rindex(const char *s, int c);

... (truncated)

carmine lance
#

also preferably you should make a build dir and build your code there

karmic widget
# drowsy girder

i'd read that if you want to work with strings in C; it made things so much easier for me

spare wyvern
carmine lance
#

thanks

#
char **toArray(LinkedList *lista) {
    printf("toArray\n");
    // String *lines = (String *)malloc(sizeof(String) * lista->len);
    char **lines = malloc(sizeof(char *) * lista->len);
    
    Node *sup = lista->start;
    int count = 0;
    while (sup != NULL) {
        lines[count] = (char *)malloc(sizeof(char) * sup->value->len);
        for (int i = 0; i < sup->value->len; i++) 
            lines[count][i] = sup->value->str[i];
        
        memcpy(lines[count], sup->value->str, sup->value->len - 1);
        printf("%s\n", lines[count]);
        count += 1;
        sup = sup->next;
    }

    return lines;
}
#

here the code

#

though yeah strncpy should be safer