Add a string in a char variable

Hi I am new here and this is my first question.

:)

I have one variable with name wall_temp as char
And one array with name levels

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  #include <atdio.h>

  char *levels[20][50][50][4];
  char wall_temp[10]="";

  int main()
  {

    //I want to store the "001" in the levels array at 1,1,1,1
    //I use this
    levels[1][1][1][1]="001";
    // And I want to put the "001" from levels array to wall_temp variable
    wall_temp=levels[1][1][1][1];

    //Compiler drop error
    
    return 0;
    exit(0);

  }
  


But this compiler drops error.

52 C:\Eob4c\Draw_Walls_North.h incompatible types in assignment of `char*' to `char[10]'

How to store in the wall_temp variable alements from my levels array
both are char type.

char *levels[20][50][50][4];
char wall_temp[10]="";

One of them has pointer
If I change both of them with pointer

char *levels[20][50][50][4];
char *wall_temp[10]="";

Compiler drops other error

52 C:\Eob4c\Draw_Walls_North.h incompatible types in assignment of `char*' to `char*[10]'
56 C:\Eob4c\Draw_Walls_North.h comparison between distinct pointer types `char*' and `char**' lacks a cast
60 C:\Eob4c\Draw_Walls_North.h cannot convert `char**' to `char*' for argument `1' to `char* strcat(char*, const char*)'

I try to find one way to store the data from array in the wall_temp viariable.

Thank you very much.

P.S. The file Draw_Walls_North.h is my file which contains this similar code like above , but I made a simple program to present my problem.
Last edited on
wall_temp is an array. You are trying to assign a string to an array directly. You should assign all the chars from the string to each place in the array individual.
For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;
char *levels;
char wall_temp[10]="";

int main()
{
   levels="001";
   for(int i=0; levels[i]; i++){//Assign each char of the string individual
      wall_temp[i] = levels[i];//Access the pointer like being a array
      cout << wall_temp[i];//display the data in wall_temp[i] that just assigned
   }
   return 0;
}


Also the exit(0); after return 0; of the main will never be executed.
[EDIT] for(int i=0; levels[i]; i++)
I use as condition to exit the loop the levels[i] because the last char of the string is gonna be 0 (arithmetic 0). So when the loops reach the last char of the string it will exit.
Last edited on
the levels is the four dimensional array
and the wall_temp is only one string which cointains max 10 characters.

The array levels is a big map with walls
levels[20][50][50][4];
means levels[floors][map_x][map_y][wall_side];
I put a "001" at the levels[1][1][1][1]; to test my element
I mean I put on all levels on the floor 1 at map_x 1 and at map_y 1 one wall side with value=1
This means , I put one wall in these coordinates.


I thing I solve my problem with this way.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
    //---In preview code I had my wall_temp[10]=""; variable empty-------
    //---With strcpy I put the 001 from floor 1 of map_x and map_y 1 and side 1
    strcpy(wall_temp,levels[party.level][party.x][party.y-1][1]);

    //----I am checking if my front square is not empty not "000"------------
    if (levels[party.level][party.x][party.y-1][1]!="000")
    {
        //---- I checking if my front square is equal with my front square
        //--- In fact I want check if my front square if equal with wall_temp variable
        //if (levels[party.level][party.x][party.y-1][1]==wall_temp)
        
        if (levels[party.level][party.x][party.y-1][1]==levels[party.level][party.x][party.y-1][1])
        {
                                                                 
          //---I put the "W" in one new variable wall_w and I store it to wall_string variable
          strcpy(wall_string,wall_w);
          //---Then I adding (appended my front square)
          strcat(wall_string,levels[party.level][party.x][party.y-1][1]);
          //---And I adding the string "_04" appended at the end of string
          strcat(wall_string, "_04");
          //--- So I have the wall_string variable as "W001_04" this is my element of graphics sprite which I load from my data file an put it on my screen
           
           graphic = load_datafile_object("Bricks.dat",wall_string);
           draw_sprite((BITMAP*)active_page,(BITMAP*)graphic->dat,62,30);
           
           
        }
    }
   



Is there any way to put the 001 to wall_temp without this big code

1
2
    strcpy(wall_temp,levels[party.level][party.x][party.y-1][1]);
    


Something like this:
1
2
    wall_temp="001";
    

or without strcpy
1
2
    wall_temp=levels[party.level][party.x][party.y-1][1];
    


In fact my datafile contains a lots of wall kinds grahics as
W001_04
W001_05
W002_04
if my front wall is 001 I put wall kind 001 if the wall is one square in front of me I add _04 at the end in the front wall is not front but front and one square to the left I put _03 at the end.

Each Kind of walls 001 , 002 etc , have positions on the screen from _01 to _23.

This is the engine how my game renders my Dungeon Crawler walls in the labyrinth.

Thank you for very fast answer. :)

Last edited on
wall_temp is an array. So you have to use the array syntax "array[position]"
I changed the level to single pointer just to use it as an example. You can use it in an array of pointers also:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;
char *levels[20][50][50][4];
char wall_temp[10]="";

int main()
{
   levels[1][1][1][1]="001";
   for(int i=0; levels[1][1][1][1][i]; i++){//Assign each char of the string individual
      wall_temp[i] = levels[1][1][1][1][i];//Access the pointer like being a array
      cout << wall_temp[i];//display the data in wall_temp[i] that just assigned
   }
   return 0;
}


Arrays and Pointers are very similar but they are not interchangeable. A pointer can act as an array, but an array can not act as a pointer. So, you can assign a pointer to an array, but not an array to a pointer. That's why you had problems with your first code you posted.

[edit] There is not an easier way than the one you use, at least not as I know.
Last edited on
Mitsakos (137)

Mitsakos are you from Greece? (Apo poy eisai bre patrida?)
I was programming with DarkBasic and many things I have use to program like Basic.

1
2
   string="My String";
    


But this is not C is Basic.

How to put some string like this.

In basic I say

1
2
   string=levels(1,1,1,1)
    


Something similar?

1
2
    wall_temp=levels[1][1][1][1];
    


Does strcpy function do that only?
You can only do it at once with strcpy().
As I sayed before you can not assign an array to a pointer (wall_temp is array and levels is pointer) so the wall_temp = levels[1][1][1][1] is illegal.
Topic archived. No new replies allowed.