AnsweredHot!Add strings in array

Author
Dimitris
Super Member
  • Total Posts : 279
  • Reward points : 0
  • Joined: 2017/11/03 10:29:26
  • Location: 0
  • Status: offline
2018/03/13 06:39:48 (permalink)
0

Add strings in array

I need to add words in array

char array[];

array="TEST1";

array="test2" and so on.....



My final result too look like char array[]={"TEST1","test2"};



But i dont want to add the values manually

array[1]=//

I need to take auto increament +1 in every value i will add inside.

#1
rodims
Super Member
  • Total Posts : 1337
  • Reward points : 0
  • Joined: 2009/02/10 11:08:59
  • Location: 51.9627, 7.6262
  • Status: offline
Re: Add strings in array 2018/03/13 07:27:22 (permalink)
#2
1and0
Access is Denied
  • Total Posts : 8337
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Add strings in array 2018/03/13 08:05:52 (permalink)
0
strcat() ?
 
Edit: Look like OP might want an array of strings.  If so, see next post by Chris.
 
post edited by 1and0 - 2018/03/13 08:39:36
#3
Chris A
Super Member
  • Total Posts : 797
  • Reward points : 0
  • Joined: 2010/07/20 04:37:07
  • Location: 0
  • Status: offline
Re: Add strings in array 2018/03/13 08:26:15 (permalink)
+1 (1)
char * array[]={"TEST1","test2"};

Don't forget the *
That defines an array of char * pointers.
You might want:
const char * array[]={"TEST1","test2"};

 
#4
NKurzman
A Guy on the Net
  • Total Posts : 16281
  • Reward points : 0
  • Joined: 2008/01/16 19:33:48
  • Location: 0
  • Status: offline
Re: Add strings in array 2018/03/13 08:43:49 (permalink)
0
C is not Java C#, or C++.
There is no hidden memory allocation.
You did not say which PIC you are using.  But only the PIC32 supports C++.
otherwise you will need to code for C.
"i dont want to" will not change how the C Compiler works.
 
post edited by NKurzman - 2018/03/13 09:50:11
#5
1and0
Access is Denied
  • Total Posts : 8337
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Add strings in array 2018/03/13 09:04:14 (permalink)
+1 (1)
@OP, do you want to initialize an array of strings, or do you want to write strings to an array of strings at run-time?
 
#6
Dimitris
Super Member
  • Total Posts : 279
  • Reward points : 0
  • Joined: 2017/11/03 10:29:26
  • Location: 0
  • Status: offline
Re: Add strings in array 2018/03/13 23:33:24 (permalink)
0
No i want to want to initialize an array of strings. Example i want  to add string test1. test1 will be in position 0.
But if i will not add test1 first and i will add test2, then test2 will be in position 0
#7
1and0
Access is Denied
  • Total Posts : 8337
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Add strings in array 2018/03/14 02:21:19 (permalink)
+1 (3)
dimis1989
No i want to want to initialize an array of strings. Example i want  to add string test1. test1 will be in position 0.
But if i will not add test1 first and i will add test2, then test2 will be in position 0

I assume English is not your first language. I have no idea what you are talking about.
#8
moser
Super Member
  • Total Posts : 388
  • Reward points : 0
  • Joined: 2015/06/16 02:53:47
  • Location: Germany
  • Status: offline
Re: Add strings in array 2018/03/14 04:33:19 (permalink) ☼ Best Answerby dimis1989 2018/03/14 04:40:04
+2 (2)
If you want to create an array of strings, then the strings must have a fixed maximum width.
This has to be done if, if you want to be able to change characters within the strings.

#define NUMBER_OF_STRINGS 3
#define MAXIMUM_STRING_LENGTH_PLUS_ONE 32
char string_array[NUMBER_OF_STRINGS][MAXIMUM_STRING_LENGTH_PLUS_ONE ] = {
"a string",
"another string",
"and again a string"
};

Here the strings are used for initializing the array. Each of the strings is placed in the array and the rest to MAXIMUM_STRING_LENGTH_PLUS_ONE is filled with '\0' characters.
 
The array size here is 3*32 bytes, which are stored in RAM, and a second time in ROM for the initialization. All strings must be of a maximum length MAXIMUM_STRING_LENGTH_PLUS_ONE - 1 = 31 because of the terminating '\0' character. If a string is larger than 32 chars then the compiler will throw an error. But the real danger is, if any of the strings is exactly 32 chars, because then it will be without '\0' termination. You can fix that if you include the '\0' in every string used in the initialization explicitly, for example "a string\0".
 
 
 
If you only work with constant strings, and you will never change single characters within a string, then you should work with pointers to constant strings instead.
 

#define NUMBER_OF_STRINGS 3
char const * stringpointer_array[NUMBER_OF_STRINGS] = {
"a string",
"another string",
"and again a string"
};

Here the strings only represent a pointer, because you are initializing a char const * type.
 
The array size here is 3*size of a pointer = 3*4 = 12 bytes stored in RAM and a second time in ROM for initialization. The string constant are stored in ROM, with about 8+1+14+1+18+1 bytes, and you can not change them (except by flashing the ROM)
 
In this case, only the strings are constant. The pointers are not. You could replace a string for example by writing
stringpointer_array[1] = "a replacement string";

This is only a pointer assignment, not a string assignment. Do not mix it up with initializing an array with a string.
 
 
 
#9
1and0
Access is Denied
  • Total Posts : 8337
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Add strings in array 2018/03/14 04:59:52 (permalink) ☄ Helpfulby dimis1989 2018/03/14 05:01:50
+2 (2)
Excellent explanation by Moser.
 
When the string array is stored in RAM, you could change a string content for example by:
strcpy(string_array[1], "a replacement string");

or
strcpy(string_array[1], str); // where str is either an array or pointer to char string

#10
Jump to:
© 2018 APG vNext Commercial Version 4.5