0

我是 C 和 PRO*C 的初学者,需要一些帮助。我的结构如下:

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

pt_st pa_st;

然后我有:

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

然后我打开并获取光标,如下所示:

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

之后,在我的代码中的某个地方,我有:

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

但是程序的控制永远不会超出 if 条件。

我怎样才能使这项工作?!

4

3 回答 3

1

编辑:

根据评论更新。

s_no是一个指向数组的指针char。(我之前错过了这个)

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++

于 2015-05-22T11:14:56.540 回答
0

pa_st.s_no[ll_cur_rec]根据您的 struct pt_st 声明指向一个 char 变量,当涉及到您在if语句中的比较时,您实际上是在与字符串“0”进行比较。字符串“0”实际上是两个字符,即 '0' 后跟 '\0' 一个 NULL 终止符。因此,您的比较应该是 char 文字,

if (pa_st.s_no[ll_cur_rec] == '0') { }
于 2015-05-22T11:04:52.650 回答
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"
{
  ...
}
于 2015-05-22T15:15:28.890 回答