473,508 Members | 2,180 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Mixing Pointers and Integers in C

11 New Member
Hi,

I need to mix pointers and integers, but can't figure out how to do it, so if any caritative soul can help me in here I'll really really appreciate it.

How should I cast things in here?

At the end is what I'd like to do.

TIA & Regards ...


Expand|Select|Wrap|Line Numbers
  1. #include <stdlib.h>
  2.  
  3. typedef long myinteger;
  4. typedef myinteger *mypointer;
  5.  
  6. main()
  7. {
  8.     mypointer p;
  9.     myinteger myiTest;
  10.  
  11.     p=malloc(100);
  12.  
  13.     // Now I want to put in place 3 of p, a pointer to place 7 of p
  14.     p[3]=(myinteger)p+7;
  15.  
  16.     // Now I want to put in the place pointed by p[3] a pointer to place 11 of p
  17.     *(p[3])=p+11;
  18.  
  19.     // Now I want to put in the place pointed by the place pointed by p3 a number: 2
  20.     *(*(p[3]))=2;
  21.  
  22.     // Now I want to access the place pointed by the place pointed by p3
  23.     myiTest=*(*(p[3]));
  24.  
  25.     free(p);
  26. }
Mar 21 '08 #1
6 1408
weaknessforcats
9,208 Recognized Expert Moderator Expert
Have you run this through a compiler and executed using your debugger?

What do you see?
Mar 21 '08 #2
Adrian20XX
11 New Member
Have you run this through a compiler and executed using your debugger?

What do you see?
I see plenty of errors, that's why I asked how should I cast things to do this properly.

Here is the output:

>test.c
test.c(17) : error C2100: illegal indirection
test.c(17) : warning C4047: '=' : 'myinteger' differs in levels of indirection from 'mypointer'
test.c(20) : error C2100: illegal indirection
test.c(20) : error C2100: illegal indirection
test.c(23) : error C2100: illegal indirection
test.c(23) : error C2100: illegal indirection
1>Build log was saved at "file://BuildLog.htm"
1>TestPointers - 5 error(s), 1 warning(s)
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========
Mar 21 '08 #3
weaknessforcats
9,208 Recognized Expert Moderator Expert
Well, take the first on on line 17:

*(p[3])=p+11;
The error is:
test.c(17) : error C2100: illegal indirection
p is an array of long integers. So, p is a long* but p[3] is a long. That makes *(p[3]) illegal indirection since you can't dereference a long.

Instead of all this typecasting, you might consider using the fact that the array is in the eye of the beholder. That is, create an array of long and then create a pointer to that array that is a pointer to an array of long pointers:

Expand|Select|Wrap|Line Numbers
  1. long arr[12];    //big enough for 3 long*
  2. long** parr = (long**)arr;
  3.  
Now arr[2] is the 3rd long in arr and parr[2] is the 3rd long* in arr.

Of course, now you can't tell the addresses in the array from the integer values inthe array, but that's your problem.

This should remove all the typecasting.
Mar 21 '08 #4
hdanw
61 New Member
Pointers and integers are different sizes, types.

On some compilers you can do integer arithmetic on pointers to get some calculated offsets, but this is not a good practice to get into, as your code becomes less maintainable, and less portable.

Try to write things so that the compiler can understand what you are doing. Then your code will work properly.

For example :

Using a pointer to index an array;

Expand|Select|Wrap|Line Numbers
  1. int id[12];
  2. int * pint = id[0];
  3.  
  4. pint++; 
pint will point to the second int in the array after the increment, if and only if the compiler supports pointer arithmatic. Not all compilers do, and you may end up pointing to part of the first int, and part of the second.

Whenever posible, write it like this:
Expand|Select|Wrap|Line Numbers
  1. int id[12];
  2. int index = 0;
  3. index ++;
When you pass index in to the subscript operator like this:

id[index]; the compiler can set up checks to see that the index does not go beyond the 12 item length of the array.

using pint in the previous example does not, and sooner or later you will end up with programming bugs that are difficult to locate and resolve.

Good luck,

Happy Coding.

Dan -
Mar 21 '08 #5
Adrian20XX
11 New Member
Well, take the first on on line 17:



The error is:


p is an array of long integers. So, p is a long* but p[3] is a long. That makes *(p[3]) illegal indirection since you can't dereference a long.

Instead of all this typecasting, you might consider using the fact that the array is in the eye of the beholder. That is, create an array of long and then create a pointer to that array that is a pointer to an array of long pointers:

Expand|Select|Wrap|Line Numbers
  1. long arr[12];    //big enough for 3 long*
  2. long** parr = (long**)arr;
  3.  
Now arr[2] is the 3rd long in arr and parr[2] is the 3rd long* in arr.

Of course, now you can't tell the addresses in the array from the integer values inthe array, but that's your problem.

This should remove all the typecasting.
Thank you sooooo much, you pointed me in the right direction and now my program is working perfectly.

Your post made me re-think what I was doing, and I understood that the array was a space of memory (I like space of memory better than array because array makes you think about a type definition that might not be the best for the problem) of either pointers or integers, and that I had first to get a proper type definition for the way I will access the space of memory later.

So now, my type definition for accessing it is:
typedef long *******mytype;

As I really want to access the long that is pointed by the seventh pointer, my program is working perfectly, and I'm a happy man, thank you sooooo much again.
Mar 22 '08 #6
weaknessforcats
9,208 Recognized Expert Moderator Expert
Yes, indeed.

I wrote an article titled Arrays Revealed inthe C/C++ HowTos forum because there were a lot of questions about array structures an a general non-awareness that an array is just a space of memory, as you put it.
Mar 23 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

388
21415
by: maniac | last post by:
Hey guys, I'm new here, just a simple question. I'm learning to Program in C, and I was recommended a book called, "Mastering C Pointers", just asking if any of you have read it, and if it's...
79
3333
by: Me | last post by:
Just a question/observation out of frustration. I read in depth the book by Peter Van Der Linden entitled "Expert C Programming" (Deep C Secrets). In particular the chapters entitled: 4: The...
2
2216
by: Thomas G. Marshall | last post by:
Arthur J. O'Dwyer <ajo@nospam.andrew.cmu.edu> coughed up the following: > On Thu, 1 Jul 2004, Thomas G. Marshall wrote: >> >> Aside: I've looked repeatedly in google and for some reason cannot >>...
28
3031
by: ziman137 | last post by:
Hello all, I have a question and am seeking for some advice. I am currently working to implement an algorithmic library. Because the performance is the most important factor in later...
62
4945
by: onkar | last post by:
Why use pointers at all??
96
4114
by: subramanian | last post by:
I have been thinking that all pointers(to any obejct) have the same size. The size of a pointer is the size of an int. This is beause a memory location is addressed by an int. Is that right/wrong?
4
3494
by: Josefo | last post by:
Hello, is someone so kind to tell me why I am getting the following errors ? vector_static_function.c:20: error: expected constructor, destructor, or type conversion before '.' token...
6
3497
by: M Turo | last post by:
Hi, I was wondering if anyone can help. I'm want to pre-load a 'table' of function pointers that I can call using a its arrayed index, eg (non c code example) pFunc = func_A; pFunc = func_B;
160
5481
by: raphfrk | last post by:
Is this valid? int a; void *b; b = (void *)a; // b points to a b += 5*sizeof(*a); // b points to a a = 100;
0
7336
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7401
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7063
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7504
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5640
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5059
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4720
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1568
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
773
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.