| 
	
	|  |  |  
	
		| View previous topic :: View next topic |  
		| Author | Message |  
		| BRedding 
 
 
 Joined: 23 May 2022
 Posts: 2
 
 
 
			    
 
 | 
			
				| Using multidimensional string arrays |  
				|  Posted: Mon May 23, 2022 4:57 am |   |  
				| 
 |  
				| I am trying to solve a problem of storing a menu in ROM and them using that to print to an LCD. 
 I have created a function:
 print_lcd(char *str, int row, int col, ...)
 
 This func works in the following code:
 
  	  | Code: |  	  | rom char single_string[22] = "This is a string";
 print_lcd(single_string, 0, 0);
 
 | 
 
 but not in this code:
 
  	  | Code: |  	  | rom char multi_string[2][2][22] = {{"First string line",
 "Second string line"},
 {"Third string line",
 "Final string line"}};
 print_lcd(multi_string[0][0],0,0);
 print_lcd(multi_string[0][1],1,0);
 print_lcd(multi_string[1][0],2,0);
 print_lcd(multi_string[1][1],3,0);
 
 | 
 
 It's not that the program won't compile, it just doesn't print anything to the lcd.
 
 I assume I have made an enormous error in understanding of pics rather than this being hardware specific but I could be wrong so I'll include version numbers and everything:
 PIC18F8722
 Compiler ver 5.105
 |  |  
		|  |  
		| PCM programmer 
 
 
 Joined: 06 Sep 2003
 Posts: 21708
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Mon May 23, 2022 5:42 am |   |  
				| 
 |  
				| I was able to make it work with the program below. It displays the following output in the MPLAB vs. 8.92 Simulator's Sim Uart1 window:
 
  	  | Quote: |  	  | First string line
 Second string line
 Third string line
 Final string line
 
 | 
 I compiled it with CCS vs. 5.108.
 
 Test program:
 
  	  | Code: |  	  | #include <18F46K22.h>
 #fuses NOWDT
 #use delay(internal=4M)
 #use rs232(UART1, baud=9600, ERRORS)
 
 
 rom char multi_string[4][22] = "First string line",
 "Second string line",
 "Third string line",
 "Final string line";
 
 
 void print_lcd(rom char *str, int row, int col)
 {
 printf("%s\r", str);
 }
 
 //=======================================
 void main()
 {
 
 print_lcd(&multi_string[0][0],0,0);
 print_lcd(&multi_string[1][0],1,0);
 print_lcd(&multi_string[2][0],2,0);
 print_lcd(&multi_string[3][0],3,0);
 
 while(TRUE);
 }
 
 | 
 |  |  
		|  |  
		| BRedding 
 
 
 Joined: 23 May 2022
 Posts: 2
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Mon May 23, 2022 7:01 am |   |  
				| 
 |  
				| Thank you PCM. 
 The & was the bit I was missing.
 
 Could you please explain why that is needed in a const array of strings but not a single const string?
 |  |  
		|  |  
		| Ttelmah 
 
 
 Joined: 11 Mar 2010
 Posts: 19966
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Tue May 24, 2022 1:16 am |   |  
				| 
 |  
				| & means 'the address of'. 
 Now the function being called requires an address. So '&'.
 
 Key though is that in C, an array name "on it's own", is a shortcut for
 it's address. So
 
 multi_string equals &multi_string[0][0]
 
 You can always use &, but the designers of C were trying to save you
 from having to do this, when passing array addresses to functions.
 However this 'hides' from you that when passing anything but the
 whole array, you have got to remember to use &....
 |  |  
		|  |  
		|  |  
  
	| 
 
 | You cannot post new topics in this forum You cannot reply to topics in this forum
 You cannot edit your posts in this forum
 You cannot delete your posts in this forum
 You cannot vote in polls in this forum
 
 |  
 Powered by phpBB © 2001, 2005 phpBB Group
 
 |