473,378 Members | 1,156 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

"pointer to char" address restoring problem

#include <stdio.h>
#include <string.h>
#include <malloc.h>
int main(void)
{
int key = 120;
char *s = "String to Encrypt using XOR";
char *ss = s;
char echar;
char dchar;
size_t sizeofmsg = strlen(s)*sizeof(char*);

char *emsg = (char*)malloc(sizeofmsg);
char *dmsg = (char*)malloc(sizeofmsg);

char *ddmsg = dmsg;
char *eemsg = emsg;
int lenofmsg = strlen(s);
int count=0;
int i=0;

while(count++<strlen(s)){

echar = (char)(*ss^key);
emsg = &echar;
emsg++;
ss++;
}
count=0;
emsg = eemsg; // HERE when i restore the address of emsg via
eemsg, i'm not able to
// decrypt. How to restore the address of
original pointer to char ?.
i=0;
printf("\n");
while(count++<strlen(s)){

echar = *emsg;
dchar = (char)(echar ^ key);
ddmsg = &dchar;
printf(" %c",dchar);
emsg++;
ddmsg++;
}
printf("\n");
getch();
return 0;
}
Feb 21 '08 #1
3 2316
Mahesh wrote:
#include <stdio.h>
#include <string.h>
#include <malloc.h>
Not a standard C header; use <stdlib.hfor malloc(), realloc(),
free().
int main(void)
{
int key = 120;
char *s = "String to Encrypt using XOR";
char *ss = s;
char echar;
char dchar;
size_t sizeofmsg = strlen(s)*sizeof(char*);
Wrong calculation.
It should be (number of elements)*(size of the type s points to).
That is,
sizeofmsg = (strlen(s) + 1) * sizeof *s;
as s points to char and sizeof (char) is guaranteed to be 1,
you can write
sizeofmsg = strlen(s) + 1;
The "+1" stems from the string terminator that is not included
in the string length but contributes to the string's size.
>
char *emsg = (char*)malloc(sizeofmsg);
char *dmsg = (char*)malloc(sizeofmsg);
Note that the cast is unnecessary and can hide an error; around
here,
T *p = malloc(NumberOfElems * sizeof *p);
is recommended.
char *ddmsg = dmsg;
char *eemsg = emsg;
int lenofmsg = strlen(s);
int count=0;
int i=0;

while(count++<strlen(s)){

echar = (char)(*ss^key);
Note that XOR encryption of the value key leads to 0 ('\0'),
which also is the string terminator.
emsg = &echar;
You probably mean
*emsg = echar;

&echar is just that, the address of echar; you do not change
the contents of the storage area you got from malloc().
So, in every iteration, you point emsg at the same object.

Note that for s, you iterate on the copy, and for emsg, you
iterate on the "original". This can lead to problems.
emsg++;
ss++;
}
count=0;
emsg = eemsg; // HERE when i restore the address of emsg via
eemsg, i'm not able to
// decrypt. How to restore the address of
original pointer to char ?.
// style comments break easily on usenet; I recommend that you use
/**/ comments which cannot change semantics or "compilability" due
to line breaks
i=0;
printf("\n");
while(count++<strlen(s)){

echar = *emsg;
dchar = (char)(echar ^ key);
ddmsg = &dchar;
printf(" %c",dchar);
emsg++;
ddmsg++;
}
printf("\n");
getch();
return 0;
}
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Feb 21 '08 #2
On Feb 21, 12:37*pm, Michael Mair <Michael.M...@invalid.invalid>
wrote:
Mahesh wrote:
#include <stdio.h>
#include <string.h>
#include <malloc.h>

Not a standard C header; use <stdlib.hfor malloc(), realloc(),
free().
int main(void)
{
* * int key = 120;
* * char *s = "String to Encrypt using XOR";
* * char *ss = s;
* * char echar;
* * char dchar;
* * size_t sizeofmsg = strlen(s)*sizeof(char*);

Wrong calculation.
It should be (number of elements)*(size of the type s points to).
That is,
* sizeofmsg = (strlen(s) + 1) * sizeof *s;
as s points to char and sizeof (char) is guaranteed to be 1,
you can write
* sizeofmsg = strlen(s) + 1;
The "+1" stems from the string terminator that is not included
in the string length but contributes to the string's size.
* * char *emsg = (char*)malloc(sizeofmsg);
* * char *dmsg = (char*)malloc(sizeofmsg);

Note that the cast is unnecessary and can hide an error; around
here,
* T *p = malloc(NumberOfElems * sizeof *p);
is recommended.
* * char *ddmsg = dmsg;
* * char *eemsg = emsg;
* * int lenofmsg = strlen(s);
* * int count=0;
* * int i=0;
* * while(count++<strlen(s)){
* * * * echar = (char)(*ss^key);

Note that XOR encryption of the value key leads to 0 ('\0'),
which also is the string terminator.
* * * * emsg = &echar;

You probably mean
* *emsg = echar;

&echar is just that, the address of echar; you do not change
the contents of the storage area you got from malloc().
So, in every iteration, you point emsg at the same object.

Note that for s, you iterate on the copy, and for emsg, you
iterate on the "original". This can lead to problems.
* * * * emsg++;
* * * * ss++;
* * }
* * count=0;
* * emsg = eemsg; *// HERE when i restore the address of emsg via
eemsg, i'm not able to
* * * * * * * * * * * * * * // decrypt. How to restore the address of
original pointer to char ?.

// style comments break easily on usenet; I recommend that you use
/**/ comments which cannot change semantics or "compilability" due
to line breaks
* * i=0;
* * printf("\n");
* * while(count++<strlen(s)){
* * * * echar = *emsg;
* * * * dchar = (char)(echar ^ key);
* * * * ddmsg = &dchar;
* * * * printf(" %c",dchar);
* * * * emsg++;
* * * * ddmsg++;
* * }
* * printf("\n");
* * getch();
* * return 0;
}

Cheers
* Michael
--
E-Mail: Mine is an * /at/ gmx /dot/ de * address.
Thanks you so much Michael for you invaluable reply.

-
Mahesh
Feb 21 '08 #3
Mahesh wrote:
>
On Feb 21, 12:37 pm, Michael Mair <Michael.M...@invalid.invalid>
wrote:
Mahesh wrote:
#include <stdio.h>
#include <string.h>
#include <malloc.h>
Not a standard C header; use <stdlib.hfor malloc(), realloc(),
free().
int main(void)
{
int key = 120;
char *s = "String to Encrypt using XOR";
char *ss = s;
char echar;
char dchar;
size_t sizeofmsg = strlen(s)*sizeof(char*);
Wrong calculation.
It should be (number of elements)*(size of the type s points to).
That is,
sizeofmsg = (strlen(s) + 1) * sizeof *s;
as s points to char and sizeof (char) is guaranteed to be 1,
you can write
sizeofmsg = strlen(s) + 1;
The "+1" stems from the string terminator that is not included
in the string length but contributes to the string's size.
These two idioms are both worth knowing:

ptr = malloc(NMEMB * sizeof *ptr);

ptr = malloc(STRING_LENGTH + 1);

--
pete
Feb 21 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: signuts | last post by:
I am wondering what it means when a pointer is aligned? Could someone perhaps enlighten me or point me in the right direction? Thank you in advance. -- Sig
3
by: baumann | last post by:
hi all, i could not understand the "unnecessary" pointer comparison. /* 207 * min()/max() macros that also do 208 * strict type-checking.. See the 209 * "unnecessary" pointer comparison....
18
by: steve | last post by:
I'm trying to create a structure of three pointers to doubles. For which I have: typedef struct { double *lst_t, *lst_vc, *lst_ic; } last_values; I then need to allocate space for...
6
by: murgan | last post by:
Hi people, i am new to this group,this is my first query, friends i want to know the difference between "function pointer" and "pointer to a function" in c lang, so friends please send the...
4
by: C. J. Clegg | last post by:
A month or so ago I read a discussion about putting const ints in header files, and how one shouldn't put things in header files that allocate memory, etc. because they will generate multiple...
4
by: craig | last post by:
During construction of an object "parent", if you create a subobject that stores a pointer to the parent (through the "this" pointer), will that pointer be valid when the subobject is later called?...
20
by: chutsu | last post by:
I'm trying to compare between pointer and integer in an "IF" statement how do I make this work? if(patient.id != NULL){ } Thanks Chris
4
by: MetaKith | last post by:
Hi, Please have a look on the next code : struct foo{ float b; char z; }; struct foo foo_init = {234.77,"xcdy"}; struct foo...
4
by: prasad8619 | last post by:
please help me out in this regard as this is urgent to me because i stuck in the middle of a program....... here I have a program like this...... char name="rajesh"; char...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.