[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

each pointer declaration starts with a new random address so (p+1)-1!=p

Posted on 2014-08-18
13
Medium Priority
?
416 Views
Last Modified: 2014-09-14
pointers are not in numerical order
in theory:
int *p=A
print P //200
print P+1=204
print P+2=208

//Character arrays and pointers
#include<stdio.h>
#include<string.h>
void printOnScreen(const char* C)
{
	int j = 0;
	char theDisplay;
	//C[0] = 'A';
	while (*C !='\0') //C[j]=*(C+j)
	{
		theDisplay = *C;
		printf("%c", *C);
		C++;
	}
	printf("\n");
}

int main()
{
	int A[] = { 1, 2, 3, 4, 5 };
	char C[10] = "Hello";
	char value = *(C);
	char value1 = *(C + 1);
	char value2 = *(C + 2);
	char value3 = *(C + 3);
	int address = &C;
	int address1 = &C + 1;
	int address2 = &C + 2;
	int address3 = &C + 3;
	int *p = C;
	int *p2 = C + 2;
	int *p2Again = p + 2;
	printOnScreen(C);
	//printOnScreen(value);
	int j;
	int varC;
	for (j = 0; j < 5; j++){
		varC=C[j];
	}

}

Open in new window


in visual studios:
each address starts randomly
(see locals)
am I missing something?

pointers are not in numerical order.
0
Comment
Question by:rgb192
12 Comments
 
LVL 6

Expert Comment

by:ubound
ID: 40269240
I don't see "random," I see them counting by 10 (address: 4586140, address1: 4586150, address2: 4586160, address3: 4586170).  Since "C" is 10 bytes long, this seems right to me.  Now if you had a pointer to char (char *D), then offsets should count by 1.

Also, it is VERY bad technique to treat "pointer" and "int" as the same thing.  Consider what happens when you need to move your code to 64bit where pointers are 64bits and ints are 32bits.  Having had to find and fix bugs that come from this type of thing, I ask you to start breaking that habit today.
0
 
LVL 85

Accepted Solution

by:
ozo earned 2000 total points
ID: 40269571
200, 204, 208 looks like numerical order to me.
(p+1)-1==p
but ((int)(p+1))-1 != p
0
 
LVL 16

Expert Comment

by:HooKooDooKu
ID: 40270980
First of all, on my C/C++ compiler, your code doesn't even compile:
int address = &C; ==> Error 'cannot convert from 'char(*)[10]' to 'int'
int *p = C; ==> Error 'cannot convert from 'char [10]' to 'int*';

(This is probably in line with Expert ubound's comment that it is very bad technique to treat pointers and int as the same thing).

The other half of your issue I believe is what is known as pointer arithmetic.  When you try to add the value 'one' to a pointer, the result is not the memory address stored in the pointer plus one byte.  Instead, it is the address stored in the pointer plus the size of the data type.

As an example, check out this screen capture:
Pointer ArithmeticSince a char is one byte, the address of pA + 1 is one byte beyond pA.
Since a WORD is two bytes, the address of pB + 1 is two bytes beyond pB.
Since a DWORD is four bytes, the address of pC + 1 is four bytes beyond pC.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:rgb192
ID: 40271402
I can not test screenshot because
c
does not have
wrod
dword
0
 
LVL 16

Expert Comment

by:HooKooDooKu
ID: 40276189
You can substitute int, long, double, and other variable types for WORD and DWORD to see similar results.  
I only choose WORD and DWORD because a compiler that understands what these datatypes are (usually via a #define) will always define WORD to be a 16-bit integer and DWORD to be a 32-bit integer.

But you don't have that kind of guarantee of data size with int and long.  In the 'C' standard, you're only guaranteed that a long will be at least as big as an int and perhaps larger.  So the only way to know how many bytes are used to hold a variable type is to utilize the sizeof() operator.  If you find that sizeof(int) = 2 and sizeof(long) = 4, then you can directly substitute int for WORD and long for DWORD in the example above.  But you might also find that sizeof(int) = 4 and sizeof(long) = 4, in which case you wouldn't see the difference like you do for WORD v. DWORD because int would be the same thing as long.

In any case, you can try using the sample above using any data type you want.  Just first use the sizeof() operator in a test before you write the sample code so that you will know what size the data type is so that you will know what sort of difference you can expect to see.
0
 

Author Comment

by:rgb192
ID: 40277450
#include<stdio.h>
#include<stdlib.h>
#include<minwindef.h>
int main()
{
	char A;
	WORD B;
	DWORD C;
	char* pA = &A;
	char* pA2 = pA + 1;
	WORD* pB = &B;
	WORD* pB2 = pB + 1;
	DWORD* pC = &C;
	DWORD* pC2 = pC + 1;
}

Open in new window


Error	1	error C1189: #error :  "No Target Architecture"	c:\program files (x86)\windows kits\8.1\include\um\winnt.h	145	1	ConsoleApplication1
	2	IntelliSense: #error directive: "No Target Architecture"	c:\Program Files (x86)\Windows Kits\8.1\Include\um\winnt.h	145	2	ConsoleApplication1
	3	IntelliSense: identifier "PCONTEXT" is undefined	c:\Program Files (x86)\Windows Kits\8.1\Include\um\winnt.h	16918	11	ConsoleApplication1
	4	IntelliSense: identifier "PSLIST_HEADER" is undefined	c:\Program Files (x86)\Windows Kits\8.1\Include\um\winnt.h	17529	11	ConsoleApplication1
	5	IntelliSense: identifier "SLIST_HEADER" is undefined	c:\Program Files (x86)\Windows Kits\8.1\Include\um\winnt.h	17537	16	ConsoleApplication1
	6	IntelliSense: identifier "PSLIST_HEADER" is undefined	c:\Program Files (x86)\Windows Kits\8.1\Include\um\winnt.h	17544	13	ConsoleApplication1
	7	IntelliSense: identifier "PSLIST_HEADER" is undefined	c:\Program Files (x86)\Windows Kits\8.1\Include\um\winnt.h	17551	13	ConsoleApplication1
	8	IntelliSense: identifier "PSLIST_HEADER" is undefined	c:\Program Files (x86)\Windows Kits\8.1\Include\um\winnt.h	17559	13	ConsoleApplication1
	9	IntelliSense: identifier "PSLIST_HEADER" is undefined	c:\Program Files (x86)\Windows Kits\8.1\Include\um\winnt.h	17569	13	ConsoleApplication1
	10	IntelliSense: identifier "PSLIST_HEADER" is undefined	c:\Program Files (x86)\Windows Kits\8.1\Include\um\winnt.h	17576	10	ConsoleApplication1

Open in new window

0
 
LVL 16

Expert Comment

by:HooKooDooKu
ID: 40278831
Looks like the header files you've included what to know what version of Windows you are trying to compile for.

It would be helpful to know what version of visual studio you are using... but until then, the following MIGHT help (don't know, just guessing based on trying to create console applications in some versions of Visual Studio I have available).

Insert the following lines BEFORE your #include statement
#ifndef _WIN32_WINNT		// Allow use of features specific to Windows XP or later.                   
#define _WIN32_WINNT 0x0501	// Change this to the appropriate value to target other versions of Windows.
#endif						

Open in new window

0
 

Author Comment

by:rgb192
ID: 40291641
#ifndef _WIN32_WINNT		// Allow use of features specific to Windows XP or later.                   
#define _WIN32_WINNT 0x0501	// Change this to the appropriate value to target other versions of Windows.
#endif		
#include<stdio.h>
#include<stdlib.h>
#include<minwindef.h>
int main()
{
	char A;
	WORD B;
	DWORD C;
	char* pA = &A;
	char* pA2 = pA + 1;
	WORD* pB = &B;
	WORD* pB2 = pB + 1;
	DWORD* pC = &C;
	DWORD* pC2 = pC + 1;
}

Open in new window



Error	1	error C1189: #error :  "No Target Architecture"	c:\program files (x86)\windows kits\8.1\include\um\winnt.h	145	1	ConsoleApplication1
	2	IntelliSense: #error directive: "No Target Architecture"	c:\Program Files (x86)\Windows Kits\8.1\Include\um\winnt.h	145	2	ConsoleApplication1
	3	IntelliSense: identifier "PCONTEXT" is undefined	c:\Program Files (x86)\Windows Kits\8.1\Include\um\winnt.h	16918	11	ConsoleApplication1
	4	IntelliSense: identifier "PSLIST_HEADER" is undefined	c:\Program Files (x86)\Windows Kits\8.1\Include\um\winnt.h	17529	11	ConsoleApplication1
	5	IntelliSense: identifier "SLIST_HEADER" is undefined	c:\Program Files (x86)\Windows Kits\8.1\Include\um\winnt.h	17537	16	ConsoleApplication1
	6	IntelliSense: identifier "PSLIST_HEADER" is undefined	c:\Program Files (x86)\Windows Kits\8.1\Include\um\winnt.h	17544	13	ConsoleApplication1
	7	IntelliSense: identifier "PSLIST_HEADER" is undefined	c:\Program Files (x86)\Windows Kits\8.1\Include\um\winnt.h	17551	13	ConsoleApplication1
	8	IntelliSense: identifier "PSLIST_HEADER" is undefined	c:\Program Files (x86)\Windows Kits\8.1\Include\um\winnt.h	17559	13	ConsoleApplication1
	9	IntelliSense: identifier "PSLIST_HEADER" is undefined	c:\Program Files (x86)\Windows Kits\8.1\Include\um\winnt.h	17569	13	ConsoleApplication1
	10	IntelliSense: identifier "PSLIST_HEADER" is undefined	c:\Program Files (x86)\Windows Kits\8.1\Include\um\winnt.h	17576	10	ConsoleApplication1

Open in new window

0
 

Author Comment

by:rgb192
ID: 40307093
Did I do the correct code?
0
 
LVL 9

Expert Comment

by:Subrat (C++ windows/Linux)
ID: 40320424
Why including #include<minwindef.h>
You try with including windows.h
#include "windows.h"
0
 

Author Comment

by:rgb192
ID: 40321812
Why including #include<minwindef.h>
#include<minwindef.h>
recommended by expert HooKooDooKu
in a previous question because I can not even use
DWORD
WORD

without seeing code underlined in error unless I write: #include<minwindef.h>


#include<stdio.h>
#include<stdlib.h>
#ifndef _WIN32_WINNT		// Allow use of features specific to Windows XP or later.                   
#define _WIN32_WINNT 0x0501	// Change this to the appropriate value to target other versions of Windows.
#endif		
#include<minwindef.h>
#include<windows.h>
int main()
{
	char A;
	WORD B;
	DWORD C;
	char* pA = &A;
	char* pA2 = pA + 1;
	WORD* pB = &B;
	WORD* pB2 = pB + 1;
	DWORD* pC = &C;
	DWORD* pC2 = pC + 1;
}

Open in new window








Error	1	error C1189: #error :  "No Target Architecture"	c:\program files (x86)\windows kits\8.1\include\um\winnt.h	145	1	ConsoleApplication1
	2	IntelliSense: #error directive: "No Target Architecture"	c:\Program Files (x86)\Windows Kits\8.1\Include\um\winnt.h	145	2	ConsoleApplication1
	3	IntelliSense: identifier "PCONTEXT" is undefined	c:\Program Files (x86)\Windows Kits\8.1\Include\um\winnt.h	16918	11	ConsoleApplication1
	4	IntelliSense: identifier "PSLIST_HEADER" is undefined	c:\Program Files (x86)\Windows Kits\8.1\Include\um\winnt.h	17529	11	ConsoleApplication1
	5	IntelliSense: identifier "SLIST_HEADER" is undefined	c:\Program Files (x86)\Windows Kits\8.1\Include\um\winnt.h	17537	16	ConsoleApplication1
	6	IntelliSense: identifier "PSLIST_HEADER" is undefined	c:\Program Files (x86)\Windows Kits\8.1\Include\um\winnt.h	17544	13	ConsoleApplication1
	7	IntelliSense: identifier "PSLIST_HEADER" is undefined	c:\Program Files (x86)\Windows Kits\8.1\Include\um\winnt.h	17551	13	ConsoleApplication1
	8	IntelliSense: identifier "PSLIST_HEADER" is undefined	c:\Program Files (x86)\Windows Kits\8.1\Include\um\winnt.h	17559	13	ConsoleApplication1
	9	IntelliSense: identifier "PSLIST_HEADER" is undefined	c:\Program Files (x86)\Windows Kits\8.1\Include\um\winnt.h	17569	13	ConsoleApplication1
	10	IntelliSense: identifier "PSLIST_HEADER" is undefined	c:\Program Files (x86)\Windows Kits\8.1\Include\um\winnt.h	17576	10	ConsoleApplication1
	11	IntelliSense: identifier "PCONTEXT" is undefined	c:\Program Files (x86)\Windows Kits\8.1\Include\um\minwinbase.h	358	9	ConsoleApplication1
	12	IntelliSense: identifier "PSLIST_HEADER" is undefined	c:\Program Files (x86)\Windows Kits\8.1\Include\um\interlockedapi.h	51	11	ConsoleApplication1
	13	IntelliSense: identifier "PSLIST_HEADER" is undefined	c:\Program Files (x86)\Windows Kits\8.1\Include\um\interlockedapi.h	59	13	ConsoleApplication1
	14	IntelliSense: identifier "PSLIST_HEADER" is undefined	c:\Program Files (x86)\Windows Kits\8.1\Include\um\interlockedapi.h	67	13	ConsoleApplication1
	15	IntelliSense: identifier "PSLIST_HEADER" is undefined	c:\Program Files (x86)\Windows Kits\8.1\Include\um\interlockedapi.h	94	13	ConsoleApplication1
	16	IntelliSense: identifier "PSLIST_HEADER" is undefined	c:\Program Files (x86)\Windows Kits\8.1\Include\um\interlockedapi.h	102	10	ConsoleApplication1
	17	IntelliSense: identifier "CONTEXT" is undefined	c:\Program Files (x86)\Windows Kits\8.1\Include\um\processthreadsapi.h	762	16	ConsoleApplication1
	18	IntelliSense: identifier "PCONTEXT" is undefined	c:\Program Files (x86)\Windows Kits\8.1\Include\um\WinBase.h	1279	14	ConsoleApplication1
	19	IntelliSense: identifier "PCONTEXT" is undefined	c:\Program Files (x86)\Windows Kits\8.1\Include\um\WinBase.h	8619	13	ConsoleApplication1
	20	IntelliSense: identifier "PCONTEXT" is undefined	c:\Program Files (x86)\Windows Kits\8.1\Include\um\WinBase.h	8621	10	ConsoleApplication1
	21	IntelliSense: identifier "PCONTEXT" is undefined	c:\Program Files (x86)\Windows Kits\8.1\Include\um\WinBase.h	8637	11	ConsoleApplication1
	22	IntelliSense: identifier "PCONTEXT" is undefined	c:\Program Files (x86)\Windows Kits\8.1\Include\um\WinBase.h	8660	10	ConsoleApplication1
	23	IntelliSense: identifier "PCONTEXT" is undefined	c:\Program Files (x86)\Windows Kits\8.1\Include\um\WinBase.h	8669	10	ConsoleApplication1
	24	IntelliSense: identifier "PCONTEXT" is undefined	c:\Program Files (x86)\Windows Kits\8.1\Include\um\WinBase.h	8685	13	ConsoleApplication1

Open in new window

0
 

Author Closing Comment

by:rgb192
ID: 40322010
okay so this code does not work for me because it is a windows implementation?
Since I have decided to do 'c++ for dummies' tutorial (not c). I think this question may be a tangent.

And looking back
I don't see "random," I see them counting by 10 (address: 4586140, address1: 4586150, address2: 4586160, address3: 4586170).  Since "C" is 10 bytes long, this seems right to me.  Now if you had a pointer to char (char *D), then offsets should count by 1.
is the correct answer but I was looking for 200,204 because I was taking the tutorial too literally.

Thanks.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Suggested Courses

865 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question