Problems inserting values into vector pointer

1

I'm having trouble trying to perform a programming exercise that was passed to me, where you are asked to "create a vector-type pointer variable with 20 character-type occurrences".

#include <stdio.h>
#include <conio.h>

int main()
{
char *v[20];

for(int i = 0; i < 20; i++) {
char c;
v[i] = &c;
scanf("%c", &c);
*v[i] = c;
}

for(int i = 0; i < 20; i++) {
        printf("%c ", *v[i]);
}

getch();
}

It does not make any errors when compiling, but there are some problems: It exits the first for not after typing the 20 characters, but only 10 of them ; The result of printf is a lot of empty spaces

My experience with pointers is almost nil, I asked some friends who understand more, but could not find a solution.

    
asked by anonymous 27.11.2014 / 04:20

2 answers

0

I think your code should be this:

#include <stdio.h>
#include <conio.h>

int main() {
    char v[20];

    for(int i = 0; i < 20; i++) {
        char c;
        scanf("%c", &c);
        v[i] = c;
    }

    for(int i = 0; i < 20; i++) {
        printf("%c ", v[i]);
    }

    getch();
}

I tested it here and it worked. I just had to remove #include <conio.h> and put return 0; instead of getch(); .

Remember that in C every vector is a pointer, so char v[20]; is a pointer to a character. It only happens that this pointer points to an area of memory already allocated in the stack for 20 characters.

char *v[20]; is a pointer to another character pointer, which points to a memory area on the stack where 20 other pointers fit. This is not what you want!

The v[i] = &c; code is particularly dangerous. You get the memory address of the c variable and place it in the character vector. This operation has no meaning at all, in addition the size of the memory address does not fit in the position of the vector, so the resulting value will be truncated. The result will be that you are polluting your vector with garbage.

*v[i] = c; means that you get the i-th character of v , interpret it as a pointer and dereference, and set the c value to the resulting memory location. This is not what you want, you do not want to interpret the i-th character as a pointer. This is most likely to cause a segmentation flaw in the program or something else disastrous.

Now, looking at the code I've corrected, notice that you hardly need to dereference explicit pointers, just work with arrays. The only detail is scanf , where you get the c address only.

Another detail of your code: It will always try to read the 20 characters, even if what you want to type is smaller than that. To resolve this, in case you want to, you will have to verify that the user typed \n and stop.

    
27.11.2014 / 05:11
1

I've made some changes to your code, I do not know if it reaches exactly the goal, but I think it can help.

#include <stdio.h>

int main()
{
    char *v;
    char c[20];
    int i;  
    for(i = 0; i < 20; i++) {
        scanf("%c", &c[i]);
        getchar();

    }
    v = c;

    for(i = 0; i < 20; i++) {
            printf("letra  i = %c \n", *(v+i));
    }

return 0;
}

I used a pointer v pointing to a vector of char. The getchar() within for is taking the enter that is given. That's why your program was only getting half the elements, the other half was hit as enter.

In the last for , this is pointer arithmetic, v points to the first element of c , ie c[0] , so when you do *(v+i) is saying it will access the address of v + the address of i . i is not a specific value, it is a unit of the data type that v points, that is, char , so if i = 1 , v+i == c[1] because you are adding a char unit at v address %.

I hope I have helped!

    
11.12.2014 / 12:51