0

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?!

phuclv
  • 37,963
  • 15
  • 156
  • 475
Dax
  • 438
  • 1
  • 8
  • 29

3 Answers3

1

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

Community
  • 1
  • 1
Prabhu
  • 3,443
  • 15
  • 26
  • Hi! thank you for your reply. I changed it to '0', but still the control doesn't go to the if while it should. – Dax May 22 '15 at 12:15
  • That means the condition is not being met. `pa_st.s_no[ll_cur_rec]` is not `0`. Try printing the value of `pa_st.s_no[ll_cur_rec]` – Prabhu May 22 '15 at 12:22
  • You would have got that warning in your previous code with `""`. With `''` you should be fine. – Prabhu May 22 '15 at 12:45
  • No, I get it when change the code as you recommended :-) – Dax May 22 '15 at 12:55
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/78537/discussion-between-prabhu-and-dax). – Prabhu May 22 '15 at 13:35
  • I needed the * before array. if (*pa_st.s_no[ll_cur_rec] == 0) works as I want to :-) thank you! – Dax May 22 '15 at 14:00
  • As written, `s_no` is a *pointer to an array of `char`*, not an array of pointer to `char`. – John Bode May 22 '15 at 15:10
0

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') { }
Sunil Bojanapally
  • 12,528
  • 4
  • 33
  • 46
  • Hi! thank you for your reply. I changed it to '0', but still the control doesn't go to the if while it should. – Dax May 22 '15 at 12:15
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"
{
  ...
}
John Bode
  • 119,563
  • 19
  • 122
  • 198