10 Replies - 743 Views - Last Post: 10 April 2015 - 05:47 AM Rate Topic: -----

#1 gruba   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • 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   User is online

  • Code herder
  • member icon

Reputation: 6216
  • View blog
  • Posts: 21,454
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 gruba   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#4 Salem_c   User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2160
  • View blog
  • Posts: 4,225
  • 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!?
Was This Post Helpful? 0
  • +
  • -

#5 gruba   User is offline

  • D.I.C Head

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

Re: C: Pointers question

Posted 09 April 2015 - 04:48 AM

View PostSalem_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!?

Was This Post Helpful? 0
  • +
  • -

#6 gruba   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#7 Salem_c   User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2160
  • View blog
  • Posts: 4,225
  • 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!).
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6216
  • View blog
  • Posts: 21,454
  • Joined: 05-May 12

Re: C: Pointers question

Posted 09 April 2015 - 01:48 PM

View Postgruba, 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.
Was This Post Helpful? 0
  • +
  • -

#9 #define   User is offline

  • Duke of Err
  • member icon

Reputation: 1853
  • View blog
  • 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   


Was This Post Helpful? 0
  • +
  • -

#10 gruba   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#11 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6216
  • View blog
  • Posts: 21,454
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1