Solved

Static int pointer validation question?

Posted on 2004-08-23
11
413 Views
Last Modified: 2008-03-03
Hello Experts,

I am trying to understand static storage behavior of a small program that makes 2 calls to a function from main( ) passing macro values.
When I run this program I get output of both calls. I just want to see runSql( ) only display values from the first function call not the second one.

Here is the code:

#include<stdio.h>
#include<string.h>

#define SQL1 "select username,user_id from dba_users"
#define SQL2 "select username,user_id from dba_users_2"

/* Constants defined */
typedef enum oci {RUNSQL1=1, RUNSQL2} ociprogram;

/* Function prototypes */
int runSql(ociprogram, char *);

int main()
{
         runSql(RUNSQL1,SQL1);   /* Call to runSql - passing RUNSQL1 and SQL1 */
         runSql(RUNSQL2,SQL2);   /* Call to runSql - passing RUNSQL2 and SQL2 */
      
      return 0;
}

int runSql(ociprogram value, char *ocisql1)
{      
      static int *ptrnumber;

      if (ptrnumber == 0)
      {
            printf("RunSQL1 = %d\n",value);
            printf("SQL statment1 = %s\n",ocisql1);            
      }  

        return 0;
}

Output I am getting is:

RunSQL1 = 1
SQL statment1 = select username,user_id from dba_users
RunSQL1 = 2
SQL statment1 = select username,user_id from dba_users_2

WANT TO SEE :

RunSQL1 = 1
SQL statment1 = select username,user_id from dba_users

Can someone please help me with this?

Thanks.




0
Comment
Question by:mohammadzahid
  • 6
  • 3
  • 2
11 Comments
 
LVL 5

Expert Comment

by:lemmeC
ID: 11867786
One possible solution (possibly trivial):

int runSql(ociprogram value, char *ocisql1)
{    
     if (value == 1)
     {
          printf("RunSQL1 = %d\n",value);
          printf("SQL statment1 = %s\n",ocisql1);          
     }  

       return 0;
}
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 11867792
Hi mohammadzahid,

>         runSql(RUNSQL1,SQL1);   /* Call to runSql - passing RUNSQL1 and SQL1 */
>         runSql(RUNSQL2,SQL2);   /* Call to runSql - passing RUNSQL2 and SQL2 */

remove the second line and you will get the output you desire

Sunnycoder
0
 
LVL 5

Expert Comment

by:lemmeC
ID: 11867801
Another solution:

/* Function prototypes */
int runSql( char *);

int main()
{
        runSql(SQL1);   /* Call to runSql - passing SQL1 */
        runSql(SQL2);   /* Call to runSql - passing SQL2 */
     
       return 0;
}

int runSql(char *ocisql1)
{    
     static int number;

     if (number == 0)
     {
          printf("SQL statment1 = %s\n",ocisql1);          
     }  
    number++;

      return 0;
}


0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 11867812

Why spend the time making a call which you know will not be executed and add an additional check for all other function calls?
I feel that it is best not to make a call for something that you dont want to see ... Solution is trivial but better than alternatives
0
 
LVL 5

Assisted Solution

by:lemmeC
lemmeC earned 150 total points
ID: 11867824
int runSql(ociprogram value, char *ocisql1)
{    
     static int *ptrnumber;       //ptrnumber initialized to null

     if (ptrnumber == 0)          //Will always compare equal, because it is not being modified anywhere in this function
     {
          printf("RunSQL1 = %d\n",value);                 //Will always be executed
          printf("SQL statment1 = %s\n",ocisql1);    //Will always be executed      
     }  

       return 0;
}


Change this to:

int runSql(char *ocisql1)
{    
     static int number;          //initialized to zero

     if (number == 0)           //Compares equal the first time around. Modified later in this function. So compares unequal the second time.
     {
          printf("SQL statment1 = %s\n",ocisql1);          //Executed only once (the first time)
     }  
    number++;                  //Modify number so that the above condition will compare unequal the next time.

      return 0;
}
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 5

Expert Comment

by:lemmeC
ID: 11867834
Yet another solution (if argument list cannot be changed):

int runSql(ociprogram value, char *ocisql1)
{    
     static int number;

     if (number == 0)
     {
          printf("RunSQL1 = %d\n",value);
          printf("SQL statment1 = %s\n",ocisql1);          
     }  
    number++;

    return 0;
}
0
 
LVL 11

Author Comment

by:mohammadzahid
ID: 11867847
Thank you for the quick responses.

I can alter the condition as suggested by lemmeC and also remove the 2 call from the main and of course, that will do the trick and give me the output I want but, I am wanting to find out if I can control the execution in runSql( ) based on the static int *pointer value.

My understanding about static storage type is that when static storage class is defined the value of that variable is maintained between function calls. If first call has already set the value of the static int *pointer as defined in the runSql( ) then the next function (based on the condition defined in the if statment in my code) should skip the output of the second function call from the main but the problem is that it is not.

Thanks again, appreciate your quick responses and your time :-)

 



0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 150 total points
ID: 11867856
Hi mohammadzahid,

> first call has already set the value of the static int *pointer as defined in the runSql( )
the value is never changed

int runSql(ociprogram value, char *ocisql1)
{    
     static int *ptrnumber;

     if (ptrnumber == 0)
     {
          printf("RunSQL1 = %d\n",value);
          printf("SQL statment1 = %s\n",ocisql1);          
     }  

       return 0;
}

being a static variable with no intial value specified, it is initialized to 0 ... Hence forth, there is no change in the value and thus statements always get executed ...

Also probably you wanted to use a static int here instead of int *

int runSql(ociprogram value, char *ocisql1)
{    
     static int ptrnumber;

     if (ptrnumber == 0)
     {
          printf("RunSQL1 = %d\n",value);
          printf("SQL statment1 = %s\n",ocisql1);          
          p++;
     }  

       return 0;
}
0
 
LVL 5

Expert Comment

by:lemmeC
ID: 11867893
>>  I am wanting to find out if I can control the execution in runSql( ) based on the static int *pointer value.
Yes, you can. Just modify the pointer value in the runSql function during the first call.
Then the second time the modified value will be compared with 0, and will compare unequal.
So the printf will not be executed. The problem with this approach is that you will be left with a pointer
which may point to an illegal memory address.
So it is preferable to use static int instead of static int *, as i have mentioned in the solution
 provided above (http://www.experts-exchange.com/Programming/Q_21103269.html#11867834).
0
 
LVL 5

Expert Comment

by:lemmeC
ID: 11867930
>>My understanding about static storage type is that when static storage class is defined the value of that variable is maintained between function calls.
That's correct. The value for the static variable at the beginning of the second call to the function would be the same as the value at the end of the first call.

>> If first call has already set the value of the static int *pointer as defined in the runSql( ) then the next function (based on the condition defined in the if statment in my code) should skip the output of the >>second function call from the main but the problem is that it is not.
The first call has set the value to 0, which is the default for static variables. Also, the call does not modify the value from 0 to some other value. So when the function is called for the second time, the statement if (ptrnumber == 0) would still be true, as the value is still 0. So the printfs would be executed again.

So the best way is to use a static int in place of a static int *, and to modify the value of this static int during the first call.
0
 
LVL 11

Author Comment

by:mohammadzahid
ID: 11867959
Hi Guys,

Ok, thanks. I used the static int instead of static int *ptr and incremented the value ptr++ and now my program display output I expected.  

Thanks again for great explanation and help. I am going to split the points evenly between you and sunnycoder.

Thanks again!

Thread closed.

 
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
wefewf 2 43
wordsWithout 49 79
count7 challenge 12 69
Turning python script into an applet 12 37
This article will show, step by step, how to integrate R code into a R Sweave document
A short article about a problem I had getting the GPS LocationListener working.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now