473,566 Members | 2,812 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Passing a pointer to a function

3 New Member
Hi,

I'm coding a hash algorithm (SHA-1) in C. The algorithm works, everything compiles and I seem to be getting the correct results when I simulate it. I obviously want to automate the checking proceedure so have obtained the necessary results from "hashcalc" and included them as several 20 by 8bit arrays within my code. The hash calc algorithm returns a 5 by 32bit array to an 8bit pointer, I'm then storing the data to a 20 by 8bit array using a do-while loop before passing pointers to this array along with the array containing the known results to another function which compairs the 2 arrays as can be seen in the below code:

Expand|Select|Wrap|Line Numbers
  1. void main (void)
  2. {
  3.     uint8_T sha1_check   = 0;
  4.     uint8_T *sha1_op_ptr = 0;
  5.     uint8_T expected_sha1_result_1[20] = {0x3e,0x51,0x75,0x38,0x6b,0xe6,0x83,0xd2,0xf2,0x3,
  6. 0xf3,0xfa,0x3e,0xab,0x89,0x2a,0x79,0x90,0x82,0xf7}; /*data obtained from hash checker*/
  7.  
  8.  
  9.     sha1_op_ptr = (uint8_T *)calculateSHA1();
  10.  
  11.     do{
  12.         res[loop] = *sha1_op_ptr++;
  13.     }while(loop++ < 20);
  14.  
  15.     sha1_check = checkOutputSHA1((uint8_T *)&res[0],(uint8_T *)&expected_sha1_result[0]);
  16. }
  17.  
  18. uint32_T calculateSHA1(void)
  19. {
  20.     uint32_T hash_arr[5] = {0};
  21.  
  22.     /* Perform Hash Algorithm */
  23.  
  24.     return hash_arr;
  25. }
  26.  
  27. void checkOutputSHA1(uint8_T *hash_res_ptr,uint8_T *result_from_hash_checker_ptr);
  28. {
  29.     uint8_T matched_byte = 0;
  30.     uint8_T check_sha1_res_loop = 0;
  31.     uint8_T res[20] = 0;
  32.  
  33.  
  34.     do{
  35.         res[check_sha1_res_loop]= *hash_res_ptr;
  36.         if(*hash_res_ptr++ == *result_from_hash_checker_ptr++)
  37.         {
  38.             matched_byte++;
  39.         }
  40.     }while(check_sha1_res_loop++ < 20);    
  41.  
  42.     return matched_byte;
  43. }
  44.  
Now this works as matched_byte returns 20. However this approach is requiring me to carry out 2 do-while loops. 1 in main and 1 in checkOutputSHA1 () which seems a bit pointless when I believe I could get away with just performing a do-while in checkOutputSHA1 () if I passed sha1_op_ptr to checkOutputSHA1 () instead of res[] as such..
Expand|Select|Wrap|Line Numbers
  1. void main (void)
  2. {
  3.     uint8_T sha1_check   = 0;
  4.  
  5.     uint8_T *sha1_op_ptr = 0;
  6.  
  7.     /*data obtained from hash checker*/
  8.     uint8_T expected_sha1_result_1[20] = {0x3e,0x51,0x75,0x38,0x6b,0xe6,0x83,0xd2,0xf2,0x31,
  9.                                          0xf3,0xfa,0x3e,0xab,0x89,0x2a,0x79,0x90,0x82,0xf7}; 
  10.  
  11.  
  12.     sha1_op_ptr = (uint8_T *)calculateSHA1();
  13.  
  14.     do{
  15.         res[loop] = *sha1_op_ptr++;
  16.     }while(loop++ < 20);
  17.  
  18.     sha1_check = checkOutputSHA1(sha1_op_ptr,(uint8_T *)&expected_sha1_result[0]);
  19. }
  20.  
Unfortunately this doesn't seem to work correctly as matched_byte = 0. Any suggestions as to what I'm doing wrong would be greatly appreciated as it's driving me nuts!!! Does the function prototype for checkOutputSHA( ) need modifying as I've left it the same or am I just trying to achieve the impossible?

Cheers.
Jul 19 '07 #1
4 1591
ravenspoint
111 New Member
It looks to me that in line 18 you are passing the incremented pointer ( incremented at 15 ). Why do you not remove lines 14 to 16, since this was the point anyway?
Jul 19 '07 #2
ravenspoint
111 New Member
Another problem. In calculateSHA1() you are returning a pointer to an automatic, which will 'go away' at some point.
Jul 19 '07 #3
digidig
3 New Member
It looks to me that in line 18 you are passing the incremented pointer ( incremented at 15 ). Why do you not remove lines 14 to 16, since this was the point anyway?
opps, in my actual code I had, this was a cut and paste jobby from the previous code snippet and got left in. careless
Jul 19 '07 #4
digidig
3 New Member
Another problem. In calculateSHA1() you are returning a pointer to an automatic, which will 'go away' at some point.
So I am and indeed it does. Spotters badge for ravenspoint, problem solved :O)
Jul 19 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
16825
by: Andy Read | last post by:
Dear all, I thought I understood passing parameters ByVal and ByRef but I clearly don't! If I define a simple class of: Public Class Person Public Name as String Public Age as Integer End Class
58
10077
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of code... TCHAR myArray; DoStuff(myArray);
8
4102
by: kalinga1234 | last post by:
there is a problem regarding passing array of characters to another function(without using structures,pointer etc,).can anybody help me to solve the problem.
6
8815
by: keepyourstupidspam | last post by:
Hi, I want to pass a function pointer that is a class member. This is the fn I want to pass the function pointer into: int Scheduler::Add(const unsigned long timeout, void* pFunction, void* pParam)
17
3577
by: Charles Sullivan | last post by:
The library function 'qsort' is declared thus: void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *)); If in my code I write: int cmp_fcn(...); int (*fcmp)() = &cmp_fcn; qsort(..., fcmp); then everything works. But if instead I code qsort as:
17
2799
by: Christopher Benson-Manica | last post by:
Does the following program exhibit undefined behavior? Specifically, does passing a struct by value cause undefined behavior if that struct has as a member a pointer that has been passed to free()? #include <stdlib.h> struct stype { int *foo; };
12
5374
by: Mike | last post by:
Consider the following code: """ struct person { char *name; int age; }; typedef struct person* StructType;
6
3017
by: Roman Mashak | last post by:
Hello, I belive the reason of problem is simple, but can't figure out. This is piece of code: struct timeval { long tv_sec; /* seconds */ long tv_usec; /* microseconds */ };
8
2081
by: Ivan Liu | last post by:
Hi, I'd like to ask if passing an object as an pointer into a function evokes the copy constructor. Ivan
7
3292
by: TS | last post by:
I was under the assumption that if you pass an object as a param to a method and inside that method this object is changed, the object will stay changed when returned from the method because the object is a reference type? my code is not proving that. I have a web project i created from a web service that is my object: public class...
0
7888
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8108
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7644
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7951
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6260
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3643
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2083
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1201
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.