Welcome Guest ( Log In | Register )

Outline · [ Standard ] · Linear+

 how to write empty array?, password programming

views
     
TSikanmasin
post Nov 11 2010, 07:59 PM, updated 16y ago

Getting Started
**
Junior Member
123 posts

Joined: Dec 2008


hi all,
i am programming a chip using MPLAB HI-TECH, but stuck with the empty array.
well, the program started with i declare an array to store the password, if the array is empty then the key in password will set as password.. now my problem is how to write to know the array is empty?

partial of my programming

unsigned char keyin_char[4]; //declare an 4 values for receiving password
unsigned char stalled_char[4]; // declare an 4 values password array to store the password like this correct?
if(stalled_char[4]="NULL") // problem starts here, how to write IF array is empty then first keyin_char send to first stalled_char
{
password_count=0;
(keyin_char[0]=stalled_char[0]);
(keyin_char[1]=stalled_char[1]);
(keyin_char[2]=stalled_char[2]);
(keyin_char[3]=stalled_char[3]);
(keyin_char[4]=stalled_char[4]);
(keyin_char[5]=stalled_char[5]);
}

thanks all
Zepx
post Nov 11 2010, 08:17 PM

Regular
******
Senior Member
1,232 posts

Joined: Dec 2005
Erm... I think NULL here is a keyword.. what you are checking is a String. Also note about the double equals.

CODE

if(stalled_char[4] = "NULL") {


should be

CODE

if(stalled_char == NULL) {


Give that a try.... I'm not sure what language you are programming in though. Is this C/C++?
TSikanmasin
post Nov 11 2010, 08:28 PM

Getting Started
**
Junior Member
123 posts

Joined: Dec 2008


thanks for the reply~ array index out of bounds and undefined identifier "NULL".. this is wat i got..


Added on November 11, 2010, 8:30 pmthis is the compiler i using http://www.microchip.com/stellent/idcplg?I...ocName=en534868

This post has been edited by ikanmasin: Nov 11 2010, 08:30 PM
geekster129
post Nov 11 2010, 08:39 PM

Janitor
******
Senior Member
1,180 posts

Joined: Jan 2007
From: *awaiting GPS accuracy*



QUOTE(ikanmasin @ Nov 11 2010, 07:59 PM)
hi all,
i am programming a chip using MPLAB HI-TECH, but stuck with the empty array.
well, the program started with i declare an array to store the password, if the array is empty then the key in password will set as password.. now my problem is how to write to know the array is empty?

partial of my programming

unsigned char keyin_char[4];      //declare an 4 values for receiving password
unsigned char stalled_char[4];    // declare an 4 values password array to store the password like this correct?
if(stalled_char[4]="NULL")          // problem starts here, how to write IF array is empty then first keyin_char send to first stalled_char
{
password_count=0;
(keyin_char[0]=stalled_char[0]);
(keyin_char[1]=stalled_char[1]);
(keyin_char[2]=stalled_char[2]);
(keyin_char[3]=stalled_char[3]);
(keyin_char[4]=stalled_char[4]);
(keyin_char[5]=stalled_char[5]);
}

thanks all
*
First of all, stalled_char[4] represents a byte (character) in the stalled_char array.

Secondly, if memory serves me well, if you write out the value of stalled_char[4] if it's null, it should return 0 (as in the ASCII code), I may be wrong.

Have you tried to use strcmp function?
TSikanmasin
post Nov 11 2010, 08:45 PM

Getting Started
**
Junior Member
123 posts

Joined: Dec 2008


QUOTE(geekster129 @ Nov 11 2010, 08:39 PM)
First of all, stalled_char[4] represents a byte (character) in the stalled_char array.

Secondly, if memory serves me well, if you write out the value of stalled_char[4] if it's null, it should return 0 (as in the ASCII code), I may be wrong.

Have you tried to use strcmp function?
*
er... heard bout it before but how use tat fuction? sry noobie and newbie in programming
geekster129
post Nov 11 2010, 08:52 PM

Janitor
******
Senior Member
1,180 posts

Joined: Jan 2007
From: *awaiting GPS accuracy*



QUOTE(ikanmasin @ Nov 11 2010, 08:45 PM)
er... heard bout it before but how use tat fuction? sry noobie and newbie in programming
*
#include <string.h>

strcmp(char *a,char *b);

returns 0 if a=b
returns negative value if length of a<b
returns positive value if length of a>b
TSikanmasin
post Nov 11 2010, 09:26 PM

Getting Started
**
Junior Member
123 posts

Joined: Dec 2008


QUOTE(geekster129 @ Nov 11 2010, 08:52 PM)
#include <string.h>

strcmp(char *a,char *b);

returns 0 if a=b
returns negative value if length of a<b
returns positive value if length of a>b
*
after return then? wat i wanted is to check whether the array is empty or not.. because when a number keys in, then the array is not empty rite.. so how to check?
mgjg
post Nov 11 2010, 09:37 PM

Enthusiast
*****
Senior Member
734 posts

Joined: Jun 2010
QUOTE(ikanmasin @ Nov 11 2010, 08:28 PM)
thanks for the reply~ array index out of bounds and undefined identifier "NULL".. this is wat i got..


Added on November 11, 2010, 8:30 pmthis is the compiler i using http://www.microchip.com/stellent/idcplg?I...ocName=en534868
*
"array index out of bounds" because you tried to access beyond the declared array i.e this code
CODE
(keyin_char[5]=stalled_char[5]);


"undefined identifier "NULL"" you should check the compiler manual on how to define the 'null' character e.g NULL, null, 'null', \0, '\0' etc.
geekster129
post Nov 11 2010, 09:40 PM

Janitor
******
Senior Member
1,180 posts

Joined: Jan 2007
From: *awaiting GPS accuracy*



QUOTE(ikanmasin @ Nov 11 2010, 09:26 PM)
after return then? wat i wanted is to check whether the array is empty or not.. because when a number keys in, then the array is not empty rite.. so how to check?
*
strcmp(a,"");

If return value is 0 then it a is a null value, otherwise, a is not a null value.
TSikanmasin
post Nov 11 2010, 09:54 PM

Getting Started
**
Junior Member
123 posts

Joined: Dec 2008


QUOTE(mgjg @ Nov 11 2010, 09:37 PM)
"array index out of bounds" because you tried to access beyond the declared array i.e this code
CODE
(keyin_char[5]=stalled_char[5]);


"undefined identifier "NULL"" you should check the compiler manual on how to define the 'null' character e.g NULL, null, 'null', \0, '\0' etc.
*
thanks for the teaching.. i get it for the first one.. but the null haven.. haha... still in the mist~ rclxub.gif
mgjg
post Nov 11 2010, 10:41 PM

Enthusiast
*****
Senior Member
734 posts

Joined: Jun 2010
QUOTE(ikanmasin @ Nov 11 2010, 09:54 PM)
thanks for the teaching.. i get it for the first one.. but the null haven.. haha... still in the mist~ rclxub.gif
*
firstly:
in c++ the empty character is defined as '\0' (also 0 actually) e.g to declare an array of empty characters we can use:
CODE
char array[3] = {'\0'};

or
CODE
char array[3] = {0};

both will initialize an array of empty characters or null characters

to check for empty arrays you just use
CODE
if(array[0] == 0) or if(array[0] != '\0') etc.


but still I must stress that you read up the compiler's documentation first, there could be differences between pc and PIC compilers

secondly:
CODE
unsigned char stalled_char[4];

this is an uninitialized array in c++ it's not the same as an empty character arrays I'm talking about above, therefore:
CODE
if(char stalled_char[2] == 0) or if(char stalled_char[2] == '\0')

should fail, i.e the codes that depends on that being true will not be executed.

This post has been edited by mgjg: Nov 11 2010, 10:43 PM
dstl1128
post Nov 11 2010, 10:50 PM

Look at all my stars!!
*******
Senior Member
4,464 posts

Joined: Jan 2003
1. For C, use NULL for the safe side, and #include <stdlib.h> (the standard way). Normally in C, NULL is:
#define NULL ((void*)0)
But for embedded, it can be any other value specific to that platform - so use NULL for the safe side. And consult the 'help' for the compiler if there's no stdlib.h.


2. char a[4], means you have only 4 items
-> means valid index range is in [0, 4)
-> which means 0 <= x < 4, where x is the index.
-> which also means, 0, 1, 2, 3 only.


3. Since you are doing embedded programming, the availability of strcmp need to be questioned.


4. Some 'string' example:
"ABC" means { 'A', 'B', 'C', '\0' }

char a[] = "ABC"; /* means a has 4 characters including the hidden \0 */
char a[] = { 'A', 'B', 'C', '\0' }; /* same as above */

char a[3] = "ABC"; /* compiler could issue error or warning, depends, but if you ignore, problems later will be hard to find */

char a[7] = "ABC"; /* according to latest standard, should means.... */
char a[7] = { 'A', 'B', 'C', '\0', '\0', '\0', '\0' };
char a[7] = {}; /* means seven '\0' */
char a[7] = { '\0' }; /* means seven '\0' too */
/* so for array, when it has initialization specified, the remaining elements will be zeroed
* but embedded compiler need not always using latest C compiler (C99)
*/


5. Don't mix array & pointer together. In certain way they seems to be the same but it is not. Anyway some similarities:
(array indexing -> pointer arithmetic)
p[3] -> *(p + 3)
&p[3] -> p + 3
[3]p -> *(3 + p)
[0]p -> *(0 + p) -> *p
p[0] -> *(p + 0) -> *p

This post has been edited by dstl1128: Nov 11 2010, 11:03 PM
TSikanmasin
post Nov 11 2010, 11:38 PM

Getting Started
**
Junior Member
123 posts

Joined: Dec 2008


thanks for the useful and helpful info u all giving.. i will take time to go through it and anything i will repost again.. thanks alot and sorry for my english and my noobie programming.. cant even say out it is an embedded programming.. lol.. thanks alot~

 

Change to:
| Lo-Fi Version
0.0189sec    0.81    5 queries    GZIP Disabled
Time is now: 18th December 2025 - 06:21 AM