473,499 Members | 1,589 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 1585
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
16809
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...
58
10042
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...
8
4093
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
8806
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*...
17
3567
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...
17
2786
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...
12
5366
by: Mike | last post by:
Consider the following code: """ struct person { char *name; int age; }; typedef struct person* StructType;
6
3010
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
2074
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
3288
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...
0
7132
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7009
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7178
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,...
1
6899
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...
0
7390
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...
0
5475
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,...
1
4919
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3094
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1427
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 ...

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.