dpm24
dpm24

Reputation: 35

How to assign the for loop variable value to each element of an array in C?

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

Upvotes: 0

Views: 1889

Answers (1)

Stef1611
Stef1611

Reputation: 2387

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

Upvotes: 1

Related Questions