Here is something crazy ... maybe.

M

Morlaath

O.K. I compiled and ran the following code:

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

typedef char* bebop;
typedef unsigned char* byte_pointer;

void show_bytes(byte_pointer start, int len)
{
int i;
for(i = 0; i < len; i++)
printf(" %.2X ", start);
printf("\n");
}

void show_bits(byte_pointer start, int len)
{
int i, j;
for(i = 0; i < len; i++)
{
for(j = 0; j < 8; j++)
{
if(j % 4 == 0)
printf(" ");
if((start << j) & (0x80))
printf("1");
else
printf("0");
}
}
printf("\n");
}

int multiply(int x, int m) {
if(m == 5)
return (x << 2) + x;
else if(m == 9)
return (x << 3) + x;
else if(m == 14)
return (x << 4) - (x << 1);
else if(m == -56) {
return -((x << 6) - (x << 3));
} else printf("Bad input\n");
}



int main() {
int x;

printf("Enter an int: ");
scanf("%d", &x);

printf("%d * 5 = %d\n", x, multiply(x, 5));
printf("%d * 9 = %d\n", x, multiply(x, 9));
printf("%d * 14 = %d\n", x, multiply(x, 14));
printf("%d * -56 = %d\n", x, multiply(x, -56));

bebop bb;

int bitch = 0x00000000000000000000000000000001;
int bob = 1;

printf("BYTES for bitch: ");
show_bytes((byte_pointer)&bitch, sizeof(bitch));

printf("bebop: %.2x\n", *bb);

}

I got a seg fault on this. Here is the output:

billybob@~/homeforbillybob ./problem4
Enter an int: 5
5 * 5 = 25
5 * 9 = 45
5 * 14 = 70
5 * -56 = -280
BYTES for bitch: 01 00 00 00
Segmentation fault (core dumped)
 
K

Kojak

Le Sat, 26 Jan 2008 22:44:26 -0800 (PST),
(e-mail address removed) a écrit :
O.K. I compiled and ran the following code:

[...]
bebop bb;
[...]
printf("bebop: %.2x\n", *bb);
}

I got a seg fault on this.

Check around there (Where 'bb' point to ?).
 
M

Martin Ambuhl

O.K. I compiled and ran the following code:

[much completely irrelevant code snipped, leaving]
#include <stdio.h>
typedef char* bebop;
int main() {
bebop bb;
printf("bebop: %.2x\n", *bb);
}

I got a seg fault on this.

Of course you did. When you dereference a pointer that has never been
initialized, what do you expect?
 
A

Army1987

Morlaath said:
O.K. I compiled and ran the following code:

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

typedef char* bebop;
typedef unsigned char* byte_pointer;
I don't like to hide pointerness behind typedefs. YMMV. (But byte_pointer
is self-explanatory enough not to do any harm.)
void show_bytes(byte_pointer start, int len) {
int i;
for(i = 0; i < len; i++)
printf(" %.2X ", start);
printf("\n");
}

void show_bits(byte_pointer start, int len) {
int i, j;
for(i = 0; i < len; i++)
{
for(j = 0; j < 8; j++)

Use CHAR_BIT. Bytes aren't guaranteed to be 8 bits (though nowadays
practically any hosted implementation has 8-bit bytes).

{
if(j % 4 == 0)
printf(" ");
if((start << j) & (0x80))

I would make the loop with j starting from CHAR_BIT - 1 down to 0, and use
if (start & 1U << j). That would not depend on the size of a byte.
printf("1");
else
printf("0");
}
}
printf("\n");
}

int multiply(int x, int m) {
if(m == 5)
return (x << 2) + x;
Are you aware that this needn't work if x is negative?
else if(m == 9)
return (x << 3) + x;
else if(m == 14)
return (x << 4) - (x << 1);
else if(m == -56) {
return -((x << 6) - (x << 3));
} else printf("Bad input\n");
}



int main() {
int x;

printf("Enter an int: ");
scanf("%d", &x);
If the user enters non-number stuff, scanf fails and x stays
uninitialized.
printf("%d * 5 = %d\n", x, multiply(x, 5)); printf("%d * 9 = %d\n", x,
multiply(x, 9)); printf("%d * 14 = %d\n", x, multiply(x, 14));
printf("%d * -56 = %d\n", x, multiply(x, -56));

bebop bb;

int bitch = 0x00000000000000000000000000000001; int bob = 1;

printf("BYTES for bitch: ");
show_bytes((byte_pointer)&bitch, sizeof(bitch));

printf("bebop: %.2x\n", *bb);

}

I got a seg fault on this.
As others already pointed out, where does bb point?

And to print single characters, you can use putchar() rather than printf().
For byte counts, the commonly used type is size_t rather than int.
 
A

Army1987

Army1987 said:
Morlaath said:
void show_bytes(byte_pointer start, int len) { [snip]
show_bytes((byte_pointer)&bitch, sizeof(bitch));
Forgot to say: That cast is totally useless. And the parentheses around
bitch are unnecessary and possibly confusing (they're only required around
type names (or around expressions "binding less than" sizeof, i.e.
expressions that aren't unary-expressions, for that matter)).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top