char
to a procedure that will resize (with malloc()
), write its contents and return to main()
.
I know that every vector is a pointer and is already passed by reference, but something in my software is not letting this happen, when I go back to the function main()
it is with the initial values, it follows example code: p>
#include <stdio.h>
#include <stdlib.h>
void proc(char msg[])
{
unsigned int i;
msg = malloc(sizeof(char) * 10);
msg[0] = 'a';
msg[1] = 'b';
msg[2] = 'c';
msg[3] = 'd';
msg[4] = 'e';
msg[5] = 'f';
msg[6] = 'g';
msg[7] = 'h';
msg[8] = 'i';
msg[9] = 'j';
for (i = 0; i<10; i++) {
printf("%c\n", msg[i]);
msg[i] = 'x';
}
printf("\n");
}
int main ()
{
char msg[] = "12345";
unsigned int i;
printf("Before proc\n");
for (i = 0; i<5; i++) {
printf("%c\n", msg[i]);
}
printf("\nin proc ======\n");
proc( msg );
printf("After proc\n");
for (i = 0; i<10; i++) {
printf("%c\n", msg[i]);
}
printf("FIM");
return 0;
}
The output is as follows:
bash-4.2$ ./a.out Before proc 1 2 3 4 5 in proc ====== a b c d e f g h i j After proc 1 2 3 4 5 FIM
Where am I going wrong?