Change value of a vector in a function

3

How do I? I'm trying:

#include <stdio.h>
#include <stdlib.h>
int changeMap(char mp[], int x, int y){
    mp[x-1][y-1] = 'X';
return 0;
}
int main(){
    char map[][1] = "gabriel";
    printf("%c", map[0][0]);
    changeMap(map, 1, 1);
    printf("%c", map[0][0]);
return 0;
}

You want to return:

t
X

But returns:

G:\PROJETOS\C\main.c||In function 'changeMap':|
G:\PROJETOS\C\main.c|4|error: subscripted value is neither array nor pointer nor vector|
G:\PROJETOS\C\main.c||In function 'main':|
G:\PROJETOS\C\main.c|8|error: invalid initializer|
G:\PROJETOS\C\main.c|10|warning: passing argument 1 of 'changeMap' from incompatible pointer type [enabled by default]|
G:\PROJETOS\C\main.c|3|note: expected 'char *' but argument is of type 'char (*)[1]'|
||=== Build failed: 2 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|
    
asked by anonymous 17.12.2014 / 08:18

1 answer

7

You have in this code some things wrong:

  • You need to initialize the character map before using it;
  • You can not assign a string to a character;

    char map[][1] = "gabriel";

Code:

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

#define N 10

int changeMap(char mp[N][N], int x, int y)
{
    mp[x-1][y-1] = 'X';
    return 0;
}
int main()
{       
    char map[N][N];  //inicialização do map de N por N
    map[0][0]= 'T';  //atribuição do 'T' a posição x=0, y=0

    printf("%c", map[0][0]);
    changeMap(map, 1, 1);
    printf("%c", map[0][0]);
    return 0;
}

Result:

TX

Running on ideone

    
17.12.2014 / 10:32