简体   繁体   中英

Why the control of my PRO*C program doesn't go to the if section?

I am beginner in C and PRO*C and need some help. I have a structure as below:

typedef struct pt_st{
  char (*s_no)[NULL_BIG_SEQ_NO];
  char (*s)[NULL_STORE];
} pt_st;

pt_st pa_st;

Then I have:

   EXEC SQL DECLARE c_st CURSOR FOR
   SELECT 5 as s, nvl(null, 0) as s_no
   FROM dual;

Then I open and fetch the cursor as below:

EXEC SQL OPEN c_st;
EXEC SQL FETCH c_st INTO :pa_st.s, :pa_st.s_no;

afterwards, somewhere in my code I have:

    if (pa_st.s_no[ll_cur_rec] == "0") 
    {
        // do something here, but the control of the program never reaches here!!!  
    }

But the control of the program never goes iside the if condition.

How can I make this work?!

EDIT:

Updated based on comments.

s_no is a pointers to an array of char . ( I missed this earlier)

You are comparing pointer with "0" which is a pointer to a null terminated string. "0" is a string with '0' and a NULL terminator. No warnings here. But incorrect comparison nonetheless.

You are possibly wanting to dereference the char pointer at ll_cur_rec and see if it equals '0'.

if ((*pa_st.s_no)[ll_cur_rec] == '0')

Also, check this : Single quotes vs. double quotes in C or C++

pa_st.s_no[ll_cur_rec] points to a char variable as per the your struct pt_st declaration and when it comes to your comparison in if statement you are actually comparing with a string "0". String "0" is actually two characters being '0' followed by '\\0' a NULL terminator. Hence your comparison should be with char literals as,

if (pa_st.s_no[ll_cur_rec] == '0') { }

Your code is a bit confusing.

First of all, you've declared both s and s_no as pointers to arrays of char , not arrays of pointer to char . Is that what you intended? Given that both 5 and the result of nvl(null,0) will be integers, why not declare those fields as integers, such as:

typedef struct pt_st{
   int s_no
   int s;
} pt_st;

then your condition would simply be

if ( pt_st.s_no == 0 )
{
  ...
}

If you want to store string expressions from the database, declare them as VARCHAR :

VARCHAR foo[ len ];

Note that a VARCHAR has two fields - arr for storing the string contents, and len for storing the string length.

You cannot compare strings in C using the == operator. You must use a library function like strcmp or strncmp , such as

if ( strcmp( str, "0" ) == 0 ) // str is equal to the string "0"
{
  ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM