• C

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

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.
LVL 1
rgb192Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

uboundCommented:
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
ozoCommented:
200, 204, 208 looks like numerical order to me.
(p+1)-1==p
but ((int)(p+1))-1 != p
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
HooKooDooKuCommented:
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
Increase Security & Decrease Risk with NSPM Tools

Analyst firm, Enterprise Management Associates (EMA) reveals significant benefits to enterprises when using Network Security Policy Management (NSPM) solutions, while organizations without, experienced issues including non standard security policies and failed cloud migrations

rgb192Author Commented:
I can not test screenshot because
c
does not have
wrod
dword
0
HooKooDooKuCommented:
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
rgb192Author Commented:
#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
HooKooDooKuCommented:
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
rgb192Author Commented:
#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
rgb192Author Commented:
Did I do the correct code?
0
Subrat (C++ windows/Linux)Software EngineerCommented:
Why including #include<minwindef.h>
You try with including windows.h
#include "windows.h"
0
rgb192Author Commented:
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
rgb192Author Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.