473,666 Members | 2,367 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Comparing Pointer types in C

5 New Member
Hi,

I have this program in C, and have commented each line as per my understanding. At the end, I have some questions which I havent been able to understand. Any help would be appreciated.

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3.  
  4. #define Size1 (4)
  5. #define Size2 (3)
  6. #define Size3 (5)
  7.  
  8. int main() {
  9.   int* a1 = malloc(4 * sizeof(int)); /*16 bytes*/
  10.   int* a2 = malloc(3 * sizeof(int)); /*12 bytes*/
  11.   int* a3 = malloc(5 * sizeof(int)); /*20 bytes*/
  12.   a1[0] = 1; a1[1] = 2; a1[2] = 3; a1[3] = 4; /*filling array a1*/
  13.   a2[0] = 9; a2[1] = 8; a2[3] = 7; /*filling array a2*/
  14.   a3[0] = -1; a3[1] = -2; a3[2] = -3; a3[3] = -4; a3[4] = -5; /*filling array a3*/
  15.  
  16.   int sizes[ ] = {Size1, Size2, Size3, 0}; /*local array of sizes*/
  17.  
  18.   int** ptr2ptr = malloc(sizeof(int*) * 4); /*array of 16 bytes*/
  19.   ptr2ptr[0] = a1; /*storing the pointer to a1 in first element*/
  20.   ptr2ptr[1] = a2; /*storing the pointer to a2 in second element*/
  21.   ptr2ptr[2] = a3; */storing the pointer to 03 in second element*/
  22.   ptr2ptr[3] = 0;  /*this is the null pointer for termination*/
  23.  
  24.   int i = 0;
  25.   /*here we are looping until we dont reach the null terminator which ptr2ptr[3]*/
  26.   while (ptr2ptr[i]) {
  27.     int j;
  28.     for (j = 0; j < sizes[i]; j++) /* for each loop print out the contents of the pointer and increment i */
  29.       printf("%i ", ptr2ptr[i][j]);
  30.     printf("\n");
  31.     i++; /*increment i*/
  32.   }
  33.   return 0;
  34.  
Questions:

/*
Q1: Are a1, a2, and a3 of the same type?

Ans. I compared a1 and a2 using == it said not equal. Why? Is there a way I can compare these pointers?

Q2.Are a1 and ptr2ptr of the same type?

Ans: I tried to compare them and got the following: "warning: comparison of distinct pointer types lack a cast". Why?

Q3. If a1, a2, and a3 are considered to be arrays, are they of the same size?

Ans: I ran a sizeof() on them, but I think that is not right as here by size we mean "Length", so how do I do this?

Q4. If ptr2ptr is considered to be an array, is it the same size as any of a1, a2, or a3?

Ans: I guess if I know Q3 I will know Q4.


*/

Thanks for the assistance.
Jul 11 '08 #1
1 7316
Banfa
9,065 Recognized Expert Moderator Expert
TBH this looks very much like homework anyway here are my answers.

Q1. The question is not do they have the same value but are they the same type, so for example can you answer this question

Expand|Select|Wrap|Line Numbers
  1. int a;
  2. int b;
  3.  
  4. a = 7;
  5. b = 4;
  6.  
Are a and b the same type? If so the answer to question 1 should be obvious.

Q2. See my answer to Q1, it is talking of type not value. Answer the question and the reason you got the warning will become apparent.

Q3. Since they are actually pointers using sizeof will not help. You need to find another method.

Q4. Your guess is correct.


In general you seem to have tried to get the answer to your questions by executing code. However none of the questions can be answered in this manor. They must all be answered by examining the code and deducing the answers.
Jul 11 '08 #2

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

Similar topics

6
1895
by: Pierre McCann | last post by:
Hello: I am writing the following program in C, but I do most of my coding in C++, and am not intimately familiar with the C notation for pointers. As such, I am trying to do a comparison between the contents of a pointer and some hard-coded bounds on what the contents can be. The warning being returned during compile-time is: fre.c:47: warning: comparison between pointer and integer
19
2644
by: Dennis | last post by:
I have a public variable in a class of type color declared as follows: public mycolor as color = color.Empty I want to check to see if the user has specified a color like; if mycolor = Color.Empty then..... or if mycolor is Color.Empty then .......
5
2213
by: ma740988 | last post by:
There's a need for me to move around at specified offsets within memory. As as a result - long story short - unsigned char* is the type of choice. At issue: Consider the case ( test code ) where I'm comparing two structs. The struct test1 has information with regards to data_size and pointer to address. The struct test2 has information with regards to data_size and value. I will compare test1 and test2. For each matching data size,...
3
9608
by: sebastien.fricker | last post by:
Does anybody know if it is allowed to compare pointer using the operators '<', '<=', '>' or '>='?
20
2142
by: Bill Pursell | last post by:
This question involves code relying on mmap, and thus is not maximally portable. Undoubtedly, many will complain that my question is not topical... I have two pointers, the first of which is mmapped to a single page. I want to determine if the second is on the page. I'd like to do: #include "platform_appropriate_definition_of_PAGESIZE.h" int compare1(const char *a, const char *b)
18
605
by: william | last post by:
below is a short piece of code I wrote to testify my understanding of char *, and array. #include <stdio.h> int main() { char *str=NULL; char x="today is good!"; printf("%s", str);
25
13020
by: J Caesar | last post by:
In C you can compare two pointers, p<q, as long as they come from the same array or the same malloc()ated block. Otherwise you can't. What I'd like to do is write a function int comparable(void *p, void *q) that will take any two pointers and decide whether they can be compared or not. I really can't think how to do this - any suggestions?
7
1419
by: arnuld | last post by:
On Mon, 28 Apr 2008 07:25:53 +0000, Richard Heathfield wrote: Either you are not understanding me or I am not able to understand this <p_strcmpthing. This is your code:
13
2722
by: Andreas Eibach | last post by:
Hi, let's say I have this: #include <string.h> #define BLAH "foo" Later on, I do this:
0
8352
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8863
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8549
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6189
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4192
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4358
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2765
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 we have to send another system
2
2005
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1763
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.