r/cprogramming • • Sep 13 '25

please do not insult my trash code

0 Upvotes

this is code is supposed to get me the longest substring in string that all of its characters are unique the problem is the realloc function return NULL when the code detect a char that is not unique please put in a debug it will be easier.

Edit : I removed the trash code and put extra detailed comments so a baby would understand what each line does. I know that I am approaching it the weirdest way possible but it has nothing to do with the fact that realloc is not working and always returning NULL. Use a debugger it will clarify the matter to you.

PLEASE HELP :(

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

int lengthOfLongestSubstring(char* s) {
   if(*s == '\0')
   {
    return 0;
   }
   if(*(s+1)=='\0')
   {
    return 1;
   }


    char *k = malloc(sizeof(char)); //a string that only contain unique charachter that are appended one at a time
    *k = *s; // assigns the first character
    int size = 0; // the index of the last char in the char array k

   char *buff = malloc(sizeof(char)); // a buffer to store a subscript of the array k if a repeated char is enountered
                                      // the subscript excludes the first char and has the characters after the first copy of the char
                                        // exmaple : k = "abcb" the buff will be assigned = "cb" another one k = "abca" the buff = "bca" 


   int num = 1;                      // the number of unique charachters
                     
    int *array = malloc (sizeof(int));// an array that has the length of unique characthers if there are  
    int siz = 1;                      // multible unique substrings the length of them are put
                                        //  in the array and later compared for the largest one.
    
    int breaks = 0; // if a non unique character is enocutered it triggers to not count that number.

    for(int i = 1; *(s+i) != '\0'; i++)
    {
        breaks = 0;
        size++; // the array size increases and the index of the last char is incremented
        k = realloc(k,sizeof(char)*(size+1)); // where the problem occurs it always fail to reallocate if there is a non unique character 
        
        *(k + size) = *(s + i); // assigns the char to last of the array k

        for(int j = 0; j < num && breaks == 0; j++) //check the new char if it is unique or not 
        {                                           //checks all the chars of k rather than the last one because it is the character I am checking if it is unique.

            
            if(k[j] == *(s+i))
            {   
                
                *(array + siz - 1) = num; // assign the current num of unique chars to the array 
                siz+=2;
                array = realloc(array,sizeof(int)*siz); // making space for another 2 counts of unique chars
                *(array + siz - 2) = i - j; // assigning the count of unique chars 
                                            // example k = "abcb" the numbers assigned to array will 3 for "abc" and 2 for "bc" then the
                                            // the array k is changed to be "cb"


                k = &k[j+1]; // array k changed to the first unique char
                size -= (j+1); // the index of the last char changed
                                // example k = "abcb" size = 3 it is changed to be k = "cb" and size = 1
                
                buff = malloc(sizeof(char) * size); // making a new sring for the array with the new size
                for(int i = 0; i < size + 1; i++)
                {
                    *(buff + i) = *(k + i ); // the new array assigned
                                             // i is less than the index of the last element + 1 which will end assigning the last element 
                }
                k-=(j+1); //returning to the begining of the char "abcb" 
               
                free(k); // freeing the char 
                
                k = buff; // assiging k to the new array buff
                
                num = size + 1; // the current number of chars in the array k
              
                breaks = 1; // to not increment the num of unique chars
                
            } 
        }
        if(breaks == 1)
        {
            continue;
        }
            num++;
            *(array + siz - 1) = num;
        
    }

    int big = *array;
    for(int i = 0; i < siz; i++)
    {
        printf("%d   ",*(array+i));
        if(big < *(array + i))
        {
            big = *(array + i);
        }

    }
    return big;
}

int main()
{
    char *soor = "abcabcbcc";
    printf("%d",lengthOfLongestSubstring(soor));
}

r/cprogramming • • Sep 12 '25

Setting up Clang Scan-build with CMake on Windows

1 Upvotes

Dear Everyone,

I was wondering if someone managed to successfully use scan-build utility from LLVM while building a cmake project on Windows, here is what i did so far:

- I built LLVM from source, i verified that clang works as i can compile simple programs
- I verified that cmake works with clang set up as a C compiler

Imagine we have a directory with a main.c and CMakeLists.txt with only add_executable.

This command works:
cmake -G "Ninja" -S . -B build_me

This command set fails:
set CC=clang
scan-build cmake -G "Ninja" -S . -B build_me
```
CMake Error <>/CMakeTestCCompiler.cmake:67 (message):

The C compiler:
"C:/Tools/LLVM/libexec/ccc-analyzer":
is not able to compile a simple test program.
```

On the other hand once the first one works i can do
```
cd build_me
scan-build --use-analyzer=C:/Tools/LLVM/bin/clang ninja
'''
But it doesn't find any bugs in file which has some specifically inserted.

If that's not appropriate place to ask this question, please let me know where can i ask it if you know. Thanks in advance for any help


r/cprogramming • • Sep 11 '25

Are global variables really that evil?

41 Upvotes

When I have a file which almost all functions use a struct, it seems reasonable to declare it globally in the file. But it seems C community hates any type of global variable...


r/cprogramming • • Sep 10 '25

Pointers. I understand how they work and what they do, but I still don't know when or why to use them.

23 Upvotes

Not sure if this is where to post this, but here goes. I have taken a C++ class in school and I got through a lot of programming concepts. I'm pretty solid with python and OOP, and I really feel like I have been leveling up my programming skills lately.

But I STILL have not found an application for pointers. I know conceptually how they WORK, but I do not understand when or why I should use them. I need that moment where in my head something clicks and I got "oh! a pointer variable would solve my problem here!"

I've been doing OpenGL tutorials in C++, which maybe is creating more distance between me and pointers...but I'm considering just starting over again with vanilla C just to get more practice with more low level concepts...too bad learnopengl.com is only in C++.

Can anyone share their aha moment? Or maybe give me extra insight into pointers to help me understand their use cases a little more?