convert a computed string to uint32_t without coping it into a char[] buffer in c

sephora Source

I am trying to convert a substring to integer but I am getting this output

    int main()
{
    char buf[50] = "210-567-12-2040-34567890.txt";
    char* pStart = buf;
    char* pCurrent = buf;
    while(*pCurrent != '\0')
    {
        if (*pCurrent == '-' || *pCurrent == '.') 
        {
            uint32_t val = strtoul(pStart, NULL, 10); 
            pStart = pCurrent+1;
            printf("%ul\n",val);
        }
        ++pCurrent;
    }
    return 0;
}

I am getting this output

210l                                                                                                                                   
567l                                                                                                                                   
12l                                                                                                                                    
2040l                                                                                                                                  
34567890l

Why is there a l in it?

c

Answers

answered 5 months ago sudo #1

Some of the answers here are what you're looking for: What is the difference between char s[] and char *s?

Short answer is there's no difference between the two as a function argument. You can pass the char* pStart variable into strtoul the same as you'd pass the char str[20] variable into it. This will make it read the string directly from whatever memory address pStart points to, no need to copy into an array first.

comments powered by Disqus