0

I have the following code:

void word(char *a){
    size_t length = strlen(a);

    int max_cont  = 0;
    
    int size_arr = 1;
    
    for(int j = 0; j < length; j++){
        if(a[j] == ' '){
            size_arr ++;
        }
        printf("For1 : %c\n",a[j]);
    }
    
    int arr_out[size_arr];
    
    for(int y = 0; y<size_arr; y++){
        arr_out[y] = 2;
    }
    
    for(int j = 0; j < length; j++){
        if(j == 0){
            arr_out[0] = 0;
        }
        printf("Char list for 2 ##:%c\n",a[j]);
        if(a[j] == ' '){
        
            arr_out[j] = (int) j;
            
            printf("%d ---- %d\n", arr_out[j] ,j);
    
        }
    }
    
    for(int k = 0; k < size_arr; k ++){
        printf("%i FINAL -> %d\n",k, arr_out[k]);
    }
    
}

The function gets a string (*a), in which I have to look for spaces ' ', and return an array with the starting position of each word I have passed the word() function.
The first for is to get the size of the array I will create. The second one is for initialize the values. I know it is unnecessary but I was trying to see what was happening to the values. In the last for, I try to initialize the first element of the array to 0, because the there is at least a word and it do not start with space, and then try to change each value of the array for the ones the J variable gives me, showing the position.

The problem arrives when I try to print the array in the last loop. The loop shows me that the first 0 value has been set, but the rest are still 2. I do not understand what is happening.

Sorry for the poor example, the question is more oriented to what is happening than to solve the problem it self.

My output is always like this:

0 FINAL -> 0
1 FINAL -> 2
...
...
N FINAL -> 2
6
  • 2
    arr_out[j] = (int) j; You can't use j as the index for arr_out as the array will not need to be updated for every j index. Use a seperate variable for that. Commented Jan 18, 2022 at 10:53
  • 1
    General advice: Learn to use a debugger. It's not only good for finding bugs but also for understanding what the code is doing in general. Commented Jan 18, 2022 at 10:54
  • @kaylum so you propose to use for example int index variable, that increase inside the for? I have tried that an do not work. Neither I use it as index or as value. Commented Jan 18, 2022 at 13:49
  • Yes you are right, I should learn to use a C debugger but I have not time this time, It is a question that came to me while studying. Of course I will learn to use the debugger. Commented Jan 18, 2022 at 13:51
  • Not inside the for. Inside if(a[j] == ' '). That is, int out_i = 0; ... if(a[j] == ' ') { arr_out[out_i++] = (int) j; } Commented Jan 18, 2022 at 19:41

1 Answer 1

1

This is a solution written with less modifications as possible.

void word(char *a){
    size_t length = strlen(a);
    int max_cont  = 0;

    int size_arr = 1;

    for(int j = 0; j < length; j++){
        if(a[j] == ' '){
            size_arr ++;
        }
        printf("For1 : %c\n",a[j]);
    }

    int arr_out[size_arr];

    for(int y = 0; y<size_arr; y++){
        arr_out[y] = 2;
    }

    int i_arr_out=1;
    for(int j = 0; j < length; j++){
        if(j == 0){
            arr_out[0] = 0;
        }
        printf("Char list for 2 ##:%c\n",a[j]);
        if(a[j] == ' '){

            arr_out[i_arr_out] = (int) j;
            i_arr_out ++;

            printf("%d ---- %d\n", arr_out[j] ,j);

        }
    }

    for(int k = 0; k < size_arr; k ++){
        printf("%i FINAL -> %d\n",k, arr_out[k]);
    }

}

Some minor comments :
It does not work when there are two or more consecutive spaces.
The (int) cast (arr_out[i_arr_out] = (int) j;) is not usefull because j is a int and arr_out is an array of int
Some major comments :
In your code, you write outside arr_out when j becomes larger than size_arr. It may cause border effect. Try this code :

void myFoo() {
    int size_arr=5;
    int arr_out[size_arr];
    int arr_out2[size_arr];

    printf("%p\n",arr_out);
    printf("%p\n",arr_out2);

    for(int i = 0; i < size_arr; i ++){
        arr_out[i]=0;
        arr_out2[i]=0;
    }

    for(int i = 0; i < 20; i ++){
        arr_out[i]=99;
    }

    for(int i = 0; i < size_arr; i ++){
        printf("arr_out2[%i] = %d\n",i, arr_out2[i]);
    }

}

If you want to get back arr_out array after the word function call, you will have to use malloc after the size calculation of this array

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your answer, it has worked for me, I still do not understand why this happens, but at least It "works"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.