# C: Pointers question

Page 1 of 1

## 10 Replies - 716 Views - Last Post: 10 April 2015 - 05:47 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=373834&amp;s=5850ec882858afae87d561bc7cd5aba2&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 gruba

Reputation: 2
• Posts: 86
• Joined: 13-February 15

# C: Pointers question

Posted 08 April 2015 - 03:24 PM

I have a question about pointers:

If I have:
```int *p=(int *)array[0][x]
```

where array is type char array[][] and x is int

how to determine *p and p without using compiler?

This post has been edited by Skydiver: 08 April 2015 - 07:55 PM
Reason for edit:: Change some code tags to inline code tags to make the question more legible.

Is This A Good Question/Topic? 0

## Replies To: C: Pointers question

### #2 Skydiver

• Code herder

Reputation: 6067
• Posts: 20,890
• Joined: 05-May 12

## Re: C: Pointers question

Posted 08 April 2015 - 07:58 PM

Is the value of x known? Are the values inside char array[][] known? Are the dimensions of char array[][] known? Do you know if your machine is big endian or little endian? If so then you simply draw a memory map and manually do the math, as well as the binary to decimal conversion.

### #3 gruba

Reputation: 2
• Posts: 86
• Joined: 13-February 15

## Re: C: Pointers question

Posted 09 April 2015 - 12:36 AM

@Skydiver

How to determine what is the value and address of "pi" in this code:

```#define MAX 10
char array[MAX][MAX];
int x = 0;
do scanf("%s", array[x]);
while (array[x++][0] != '0');
*pi = (int*)&niz[0][7];
```

If input is:

Quote

coding is fun 0

Quote

array[0][7]
has

Quote

97'a'
value,
and pi=0x22cb97, *pi=1774964833

Is it possible to determine these values without using compiler?

@Skydiver

Sorry for mistake

Quote

niz
should be

Quote

array

### #4 Salem_c

• void main'ers are DOOMED

Reputation: 2131
• Posts: 4,196
• Joined: 30-May 10

## Re: C: Pointers question

Posted 09 April 2015 - 12:54 AM

> Is it possible to determine these values without using compiler?
No it isn't possible.

Because you're making assumptions about the endian-ess of the machine with the undefined behaviour introduced by your casting.

Even if you know the endian, the fact that you use an odd address lays you open to bus errors and/or alignment exceptions.

Who teaches this crap!?

### #5 gruba

Reputation: 2
• Posts: 86
• Joined: 13-February 15

## Re: C: Pointers question

Posted 09 April 2015 - 04:48 AM

Salem_c, on 09 April 2015 - 12:54 AM, said:

> Is it possible to determine these values without using compiler?
No it isn't possible.

Because you're making assumptions about the endian-ess of the machine with the undefined behaviour introduced by your casting.

Even if you know the endian, the fact that you use an odd address lays you open to bus errors and/or alignment exceptions.

Who teaches this crap!?

### #6 gruba

Reputation: 2
• Posts: 86
• Joined: 13-February 15

## Re: C: Pointers question

Posted 09 April 2015 - 04:55 AM

@Salem_c

Just one question. If it is not possible to determine the value of "pi" without compiler, than if using "pi" variable in another expression:
```float *pf;int t = ((*pi) & 0x41000000);
pf = (float*)&t;
printf("%5.2f\n", *pf);
```

Value of "*pf" is not possible without debugging?

This post has been edited by gruba: 09 April 2015 - 05:00 AM

### #7 Salem_c

• void main'ers are DOOMED

Reputation: 2131
• Posts: 4,196
• Joined: 30-May 10

## Re: C: Pointers question

Posted 09 April 2015 - 12:18 PM

The question is meaningless for the reasons I've already stated.

The 'code' makes laughable assumptions about the kind of machine it is running on. Without explicitly stating "This is TurboC 2.01 running on an 8086 under the DOS operating system", then any answer is just a wild guess.

Your problem is your tutor believes that this is some deep and meaningful magic which must be learnt.

To the rest of us, it's "look mum, I can hang a spoon off the end of my nose".

So go ahead, give your tutor the answer they want.

But if you ever use this code in your job, don't feel so bad if you get sacked on the spot (yes, it's that awful!).

### #8 Skydiver

• Code herder

Reputation: 6067
• Posts: 20,890
• Joined: 05-May 12

## Re: C: Pointers question

Posted 09 April 2015 - 01:48 PM

gruba, on 09 April 2015 - 03:36 AM, said:

@Skydiver

How to determine what is the value and address of "pi" in this code:

```#define MAX 10
char array[MAX][MAX];
int x = 0;
do scanf("%s", array[x]);
while (array[x++][0] != '0');
*pi = (int*)&niz[0][7];
```

If input is:

Quote

coding is fun 0

Quote

array[0][7]
has

Quote

97'a'
value,
and pi=0x22cb97, *pi=1774964833

Is it possible to determine these values without using compiler?

@Skydiver

Sorry for mistake

Quote

niz
should be

Quote

array

It is impossible for array[0][7] to contain a 'a' since the user never input a letter 'a' in the string "coding is fun 0". This is because:
```array[0][0] = 'c'
array[0][1] = 'o'
array[0][2] = 'd'
array[0][3] = 'i'
array[0][4] = 'n'
array[0][5] = 'g'
array[0][6] = ' '
array[0][7] = 'i'
array[0][8] = 's'
array[0][9] = ' '
array[1][0] = 'f'
array[1][1] = 'u'
array[1][2] = 'n'
array[1][3] = ' '
array[1][3] = '0'
array[1][4] = ??? unknown ???
:
array[9][9] = ??? unknown ???

```

Are you sure you have the correct value?

It is also very hard to determine the the address of array[0][7] unless you intimately known your compiler, linker, and OS where you tell the linker what your prefer address is for your data segment and know that the OS will respect your preferred segment address.

Most of this is basically a waste of time learning unless you are taking a class on how to perform exploits or some other niche applications. For day to day programming, you'd never really need to any of this unless you are trying to learn some parlor tricks, or you are figuring out how to hide an Easter egg in your code.

### #9 #define

• Duke of Err

Reputation: 1853
• Posts: 6,671
• Joined: 19-February 09

## Re: C: Pointers question

Posted 09 April 2015 - 04:51 PM

Hi, perhaps your are being shown it doesn't always work.

The address is fairly simple, array + 7. If you want to consider the other subscript, then the size of a row is needed eg MAXCOL, then the address is array + (0 * MAXCOL) + 7.

I'm confused about where you get 'a', although an 'a' is a byte in your integer.

It looks as if the computer is big endian and 32 bit (or greater) addressable.

On a little endian, byte addressable PC, I get this for the integer :

```  // hex    66 20 73 69
// ascii   f<sp> s  i

```

### #10 gruba

Reputation: 2
• Posts: 86
• Joined: 13-February 15

## Re: C: Pointers question

Posted 10 April 2015 - 03:10 AM

@Skydiver

For:

```#include <stdio.h>
#define MAX 10
int main()
{
char array[MAX][MAX];
int x = 0;
do scanf("%s", array[x]);
while (array[x++][0] != '0');
float* pf;
int t, *pi = (int*)&array[0][7];
t = ((*pi) & 0x41000000);
pf = (float*)&t;
printf("%5.2f\n", *pf);
return 0;
}
```

and input is: coding is fun 0

here are the variables (using NetBeans 8.0.2):

```array	{...}
array[0]	{...}
array[0][0]	99 'c'
array[0][1]	111 'o'
array[0][2]	100 'd'
array[0][3]	105 'i'
array[0][4]	110 'n'
array[0][5]	103 'g'
array[0][6]	0 '\\000'
array[0][7]	0 '\\000'
array[0][8]	1 '\\001'
array[0][9]	0 '\\000'
pf	0x22cb8c
*pf	8
t	1090519040
pi	0x22cb97
*pi	1761607936
```

In order to get the variable "*pf" without compiler do I need to use pointer arithmetic or as @Salem_c said it is a waste of time (I had this in my exam assignment)?

This post has been edited by gruba: 10 April 2015 - 03:11 AM

### #11 Skydiver

• Code herder

Reputation: 6067
• Posts: 20,890
• Joined: 05-May 12

## Re: C: Pointers question

Posted 10 April 2015 - 05:47 AM

Good! I forgot that scanf()'s string input stops at the first whitespace. That means that array[0][7]'s value can't be predicted because you didn't pre-initialize the array elements ahead of time. So even using a compiler, you still won't be able to predict the values.