943,903 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 827
  • C RSS
Apr 14th, 2009
0

dreference argv

Expand Post »
Stupid question. Haven't done production C in a while and just inherited some legacy code. In case it makes a difference it is running out of OMVS on a Z/OS IBM mainframe. The C program is invoked by an assembler program. The assembler program passes in a number arguments that are simply addresses. Some are addresses that contain addresses, and some are just addresses.

The C program then uses strcpy and memcpy to move data back and forth between the assembler program and the C program. I'm at the point where I'm trying to save some space. Currently I am copying the passed addresses to a pointer variable, and then using that variable in strcpy. For example

  1. char *cTempPtr = NULL, search[256] = "init";
  2. cTempPtr = argv[1];
  3. strcpy(search, cTempPtr);

What I would like to do is cut out the need for the extra pointer variable and just dereference the address contained in argv[1]. I have tried numerous combinations of *argv[1], etc... but apparently I just don't remember by reference/dereference logic well enough. I also haven't found any useful hits on google. To clarify I would like the code to look like:

  1. char *cTempPtr = NULL, search[256] = "init";
  2. strcpy(search, *argv[1]);

That is the basic idea anyway. Basically, the value of argv[1] is an address that points to the first byte of a null terminated character array that has been allocated in the assembler program. So, I want strcpy to use the address in argv[1] to copy that value, without having to use a middleman in the form of a pointer variable. But all my attempts to dereference argv[1] have failed. Any help will be appreciated.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
krellor is offline Offline
12 posts
since Dec 2008
Apr 14th, 2009
1

Re: dreference argv

This is the original code, right?
  1. char *cTempPtr = NULL, search[256] = "init";
  2. cTempPtr = argv[1];
  3. strcpy(search, cTempPtr);
The equivalent of that without using cTempPtr is as follows:
  1. char search[256] = "init";
  2. strcpy(search, argv[1]);
There's no magic involved between assigning cTempPtr and using it in strcpy, the address is the same. If you use *argv[1] instead, you've got a type mismatch because *argv[1] is equivalent to argv[1][0], which is the first character of the second string in argv. The second argument of strcpy expects a pointer to char, not a char.
Last edited by Narue; Apr 14th, 2009 at 4:27 pm.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Apr 14th, 2009
0

Re: dreference argv

Ok, that makes sense. But to clarify, if argv[1] holds the value "1234" will using strcpy that way that I am above cause it to copy "1234" or will it go to the address space "1234" and start copying there?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
krellor is offline Offline
12 posts
since Dec 2008
Apr 14th, 2009
1

Re: dreference argv

strcpy doesn't know or care if the string represents an address. If you want to copy the contents of the address represented by the string, you first have to convert the address to a suitable type. For example:
  1. int addr = (int)strtol ( argv[1], NULL, 0 );
  2. strcpy ( search, (char*)addr );
Exactly how you go about the conversion depends on how the address is stored in argv. It could be the string representation of an address (eg. "0x12345"), or it could be a pun of the data where each character matches a byte of the address. The above code assumes the former.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Apr 14th, 2009
0

Re: dreference argv

It is not clear to me why you try to deference argv[1]

If what you want is to take a portion of argv[1] content, you can do as follows:

strncpy(pPointer,pSource,nNumberOfBytes);




Click to Expand / Collapse  Quote originally posted by krellor ...
Stupid question. Haven't done production C in a while and just inherited some legacy code. In case it makes a difference it is running out of OMVS on a Z/OS IBM mainframe. The C program is invoked by an assembler program. The assembler program passes in a number arguments that are simply addresses. Some are addresses that contain addresses, and some are just addresses.

The C program then uses strcpy and memcpy to move data back and forth between the assembler program and the C program. I'm at the point where I'm trying to save some space. Currently I am copying the passed addresses to a pointer variable, and then using that variable in strcpy. For example

  1. char *cTempPtr = NULL, search[256] = "init";
  2. cTempPtr = argv[1];
  3. strcpy(search, cTempPtr);

What I would like to do is cut out the need for the extra pointer variable and just dereference the address contained in argv[1]. I have tried numerous combinations of *argv[1], etc... but apparently I just don't remember by reference/dereference logic well enough. I also haven't found any useful hits on google. To clarify I would like the code to look like:

  1. char *cTempPtr = NULL, search[256] = "init";
  2. strcpy(search, *argv[1]);

That is the basic idea anyway. Basically, the value of argv[1] is an address that points to the first byte of a null terminated character array that has been allocated in the assembler program. So, I want strcpy to use the address in argv[1] to copy that value, without having to use a middleman in the form of a pointer variable. But all my attempts to dereference argv[1] have failed. Any help will be appreciated.
Reputation Points: 21
Solved Threads: 1
Newbie Poster
DavidOsorno is offline Offline
14 posts
since Apr 2009
Apr 14th, 2009
0

Re: dreference argv

It is not clear to me why you
shush. grownups are talking, now.
Reputation Points: 2143
Solved Threads: 178
Posting Maven
jephthah is offline Offline
2,567 posts
since Feb 2008
Apr 14th, 2009
0

Re: dreference argv

Ok. Well, let me post a section of the original code and then my re-write of it.

Original Code
  1. int main (int argc, char *argv[])
  2. { /* 01 */
  3. FILE *fd;
  4. char host[20]="xups.uni.edu";
  5. char req[005]="init";
  6. char key[050]="init";
  7. char msg[256]="init";
  8. char nid[256]="Not Found";
  9. char uni_num[256]="Not Found";
  10. char email[256]="Not Found";
  11. char *pt1, *pt2, *pt3, *pt4, *pt5, *pt6;
  12. int *ptr_nid, *ptr_unino, *ptr_email, *ptr_msg, *ptr;
  13. int nid_adr, unino_adr, email_adr, msg_adr;
  14.  
  15. pt1 = &req[0];
  16. pt2 = &key[0];
  17. pt3 = argv[0];
  18. pt4 = argv[1];
  19. if (argc == 3) {
  20. pt3 = argv[1];
  21. pt4 = argv[2];
  22. }
  23. if (argc == 7) {
  24. pt5 = argv[6];
  25. pt6 = &host[0];
  26. strcpy (pt6,pt5);
  27. }
  28.  
  29. strcpy ( pt1,pt3);
  30. strcpy ( pt2,pt4);
  31.  
  32. query_ups(req, key, uni_num, nid, email, msg, host);


Rewrite
  1. main(int argc, char **argv)
  2. {
  3. char nid.256.=" ",
  4. uniID[256] = "",
  5. email[256]="",
  6. employeeID[256] = "",
  7. searchField[256] = "",
  8. searchValue[256] = "",
  9. *cTempPtr = NULL;
  10. int *msgPtr, *nidPtr, *uninoPtr, *emailPtr, *tempPtr;
  11. int doMemOut = 0, echo = 1;
  12.  
  13. switch(argc)
  14. {
  15. case 3:
  16. if(strcmp(argv[1], "alias") == 0)
  17. {
  18. strncpy(searchValue,argv[2], sizeof(nid) - 1);
  19. strcpy(searchField, "CN");
  20. }
  21. else if(strcmp(argv[1], "unino") == 0)
  22. {
  23. strncpy(searchValue, argv[2], sizeof(uniID) - 1);
  24. strcpy(searchField, "employeeID");
  25. }
  26. else if(strcmp(argv[1], "email") == 0)
  27. {
  28. strncpy(searchValue, argv[2], sizeof(email) - 1);
  29. strcpy(searchField, "uniEduEmailForwardingAddress");
  30. }
  31. else
  32. {
  33. printf("Specify index to search! alias,unino,email\n");
  34. return(1);
  35. }
  36. break;
  37. case 7:
  38. cTempPtr = argv[1];
  39. strcpy(searchField, cTempPtr);
  40. cTempPtr = argv[0];
  41. strcpy(searchValue, cTempPtr);
  42. cTempPtr = NULL;
  43. doMemOut = 1;
  44. echo = 0;
  45. break;
  46. default:
  47. printf("Invalid number of arguments!\n");
  48. return(1);
  49. break;
  50. }
  51.  
  52. if(query_ups(searchField,searchValue,"uniEduEmailForwardingAddress",email)!=0)

The goal here is to prep the incoming data for the query_ups function. The assembler arguments are as follows.

  1. ARGS DS 0F OMVS CALL ARGUMENTS ADDR LIST 00007700
  2. A1 DS A ARGUMENT 1 ADDR-> LDAP REQ KEY TYPE 00007800
  3. A2 DS A ARGUMENT 2 ADDR-> LDAP KEY 00007900
  4. AA3 DS A ARGUMENT 3 ADDR-> ADDR-> NETWRKID 00008000
  5. AA4 DS A ARGUMENT 4 ADDR-> ADDR-> UNINO 00008100
  6. AA5 DS A ARGUMENT 5 ADDR-> ADDR-> EMAIL 00008200
  7. AA6 DS A ARGUMENT 6 ADDR-> ADDR-> LDAP MESSAGE 00008300
  8. A7 DS A ARGUMENT 7 ADDR-> LDAP SERVER IP ADDRESS 00008400

As you can see, they pull in the data with strcpy originally. I'm just trying to clean it up and make it easier for the next unlucky to look at. To be honest, I don't know how the assembly program is passing the addresses, I don't have access to it. Just the above arg list that was given to me to go off of. I appreciate all of your help. I think the way it is working is basically the same way it was, so I'm assuming it is ok as is. But I guess I won't know until they test it and see if it gets the values from memory like it is supposed to.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
krellor is offline Offline
12 posts
since Dec 2008
Apr 15th, 2009
0

Re: dreference argv

/* you are going to have some trouble with your rewritten source at compile time, if you don't change your declaration of nid as a weird sort of structure node: it's a typo, no doubt. Also, you should put in some comments for the next person to show your logic, since they cost nothing. */
Last edited by Narue; Apr 15th, 2009 at 9:59 am. Reason: Removed email
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gnujohn is offline Offline
7 posts
since Apr 2009
Apr 15th, 2009
0

Re: dreference argv

Actually, NID is fine. The problem is that I use a TN3270 emulator to connect to the mainframe from Windows. What this means is that the hex code for [] in the mainframe is different than in the Windows world. So, when I copy back and forth I have to edit the hex values to get them to display correctly. Windows, not knowing how to display the hex code, just shows a period.

Also, I will definitely comment this code thoroughly when I am done. It is difficult to move blocks of text on the mainframe in oedit (or at least a lot clumsier than having a mouse) so I write as little as possible until I am sure it is correct/where I want it to be. I appreciate the tips though.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
krellor is offline Offline
12 posts
since Dec 2008
Apr 15th, 2009
0

Re: dreference argv

Good on both counts, and glad to try to help; interesting code.--John as gnujohn
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gnujohn is offline Offline
7 posts
since Apr 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: current directry
Next Thread in C Forum Timeline: array help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC