• C

modify code so I can see values in a local debugger window

this question is a followup to
http://www.experts-exchange.com/Programming/Languages/C/Q_28498235.html



#include <stdio.h>
#include <string.h>

main()
{
	//int A[];
	int A[] = { 2, 4, 5, 8, 1 };
	printf("%d\n", A);
	printf("%d\n", &A[0]);
	printf("%d\n", A[0]);
	printf("%d\n", *A);
	//int *p = A;
	int j;
	int addressA;
	int valueA;
	int addressAplusJ;
	int valueAplusJ;
	for (j = 0; j<5; j++)
	{
		addressA[j] = &A[j];
		valueA[j] = A[j];
		addressAplusJ[j] = &A + j;
		valueAplusJ[j] = *(A + j);

		printf("variable a[j]: address=%d value=%d\n", &A[j], A[j]);
		//printf("variable a:::: address=%d value=%d\n",&A,*A);
		printf("variable a+j:: address=%d value=%d\n", &A + j, *(A + j));
	}
}

Open in new window


I am trying to see these values in a visual studios debugger local window

            addressA[j] = &A[j];
            valueA[j] = A[j];
            addressAplusJ[j] = &A + j;
            valueAplusJ[j] = *(A + j);





all errors preventing this code from compiling:

Error	1	error C2109: subscript requires array or pointer type	c:\users\acer\documents\visual studio 2013\projects\consoleapplication1\consoleapplication1\xxx.c	20	1	ConsoleApplication1
Error	2	error C2109: subscript requires array or pointer type	c:\users\acer\documents\visual studio 2013\projects\consoleapplication1\consoleapplication1\xxx.c	21	1	ConsoleApplication1
Error	3	error C2109: subscript requires array or pointer type	c:\users\acer\documents\visual studio 2013\projects\consoleapplication1\consoleapplication1\xxx.c	22	1	ConsoleApplication1
Error	4	error C2109: subscript requires array or pointer type	c:\users\acer\documents\visual studio 2013\projects\consoleapplication1\consoleapplication1\xxx.c	23	1	ConsoleApplication1
	5	IntelliSense: expression must have pointer-to-object type	c:\Users\Acer\Documents\Visual Studio 2013\Projects\ConsoleApplication1\ConsoleApplication1\xxx.c	20	12	ConsoleApplication1
	6	IntelliSense: expression must have pointer-to-object type	c:\Users\Acer\Documents\Visual Studio 2013\Projects\ConsoleApplication1\ConsoleApplication1\xxx.c	21	10	ConsoleApplication1
	7	IntelliSense: expression must have pointer-to-object type	c:\Users\Acer\Documents\Visual Studio 2013\Projects\ConsoleApplication1\ConsoleApplication1\xxx.c	22	17	ConsoleApplication1
	8	IntelliSense: expression must have pointer-to-object type	c:\Users\Acer\Documents\Visual Studio 2013\Projects\ConsoleApplication1\ConsoleApplication1\xxx.c	23	15	ConsoleApplication1

Open in new window

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.

phoffricCommented:
Is this homework?
0
phoffricCommented:
addressA and j are int.
addressA[j] makes no sense since neither j nor addressA is an array.
0
rgb192Author Commented:
Is this homework?
personal learning. I am not enrolled in a school.

addressA[j] makes no sense since neither j nor addressA is an array.
I like using an array because more debugging local variables
What change do you suggest I should make.
0
KuppingerCole Reviews AlgoSec in Executive Report

Leading analyst firm, KuppingerCole reviews AlgoSec's Security Policy Management Solution, and the security challenges faced by companies today in their Executive View report.

phoffricCommented:
You can use an array - no problem. But j is not an array; and neither is addressA an array. The brackets, [] are used to index into an array. If you want to use brackets, then you need an array. I see that A is an array, so you can have something like A[j], which is an int.

If you have &A[j], then that is the address of the element A[j]; i.e., the address of the int.

You don't want to assign that address to addressA which is an int; instead you want to assign it to a pointer to an int.
0
rgb192Author Commented:
You don't want to assign that address to addressA which is an int; instead you want to assign it to a pointer to an int.
My experience level is just copy pasting code. I can not create pointer variables yet

okay I think the problem is 4 variables

      int addressA;
      int valueA;
      int addressAplusJ;
      int valueAplusJ;

please tell me how to define variables
and what I can do in the loop
            addressA[j] = &A[j];
            valueA[j] = A[j];
            addressAplusJ[j] = &A + j;
            valueAplusJ[j] = *(A + j);
or

            addressA = &A[j];
            valueA = A[j];
            addressAplusJ = &A + j;
            valueAplusJ = *(A + j);
0
phoffricCommented:
int * addressA = &A[j];
addressA  is now a pointer variable that is pointing to an int.

Use %x in your printf for addressA .
0
rgb192Author Commented:
#include <stdio.h>
#include <string.h>

main()
{
	//int A[];
	int A[] = { 2, 4, 5, 8, 1 };
	printf("%d\n", A);
	printf("%d\n", &A[0]);
	printf("%d\n", A[0]);
	printf("%d\n", *A);
	//int *p = A;
	int j;
	//int addressA;
	int valueA;
	int addressAplusJ;
	int valueAplusJ;
	for (j = 0; j<5; j++)
	{
		int * addressA = &A[j];
		valueA[j] = A[j];
		addressAplusJ[j] = &A + j;
		valueAplusJ[j] = *(A + j);

		printf("variable a[j]: address=%d value=%d\n", &A[j], A[j]);
		//printf("variable a:::: address=%d value=%d\n",&A,*A);
		printf("variable a+j:: address=%d value=%d\n", &A + j, *(A + j));
	}
}

Open in new window



Error	1	error MSB6006: "CL.exe" exited with code 2.	C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V120\Microsoft.CppCommon.targets	341	5	ConsoleApplication1
Error	2	error C2109: subscript requires array or pointer type	c:\users\acer\documents\visual studio 2013\projects\consoleapplication1\consoleapplication1\1.c	21	1	ConsoleApplication1
Error	3	error C2109: subscript requires array or pointer type	c:\users\acer\documents\visual studio 2013\projects\consoleapplication1\consoleapplication1\1.c	22	1	ConsoleApplication1
Error	4	error C2109: subscript requires array or pointer type	c:\users\acer\documents\visual studio 2013\projects\consoleapplication1\consoleapplication1\1.c	23	1	ConsoleApplication1
	5	IntelliSense: expression must have pointer-to-object type	c:\Users\Acer\Documents\Visual Studio 2013\Projects\ConsoleApplication1\ConsoleApplication1\1.c	21	10	ConsoleApplication1
	6	IntelliSense: expression must have pointer-to-object type	c:\Users\Acer\Documents\Visual Studio 2013\Projects\ConsoleApplication1\ConsoleApplication1\1.c	22	17	ConsoleApplication1
	7	IntelliSense: expression must have pointer-to-object type	c:\Users\Acer\Documents\Visual Studio 2013\Projects\ConsoleApplication1\ConsoleApplication1\1.c	23	15	ConsoleApplication1

Open in new window



I am defining
int * addressA = &A[j];
j times
instead of once

can you please paste the your edits of code so I can test.
0
phoffricCommented:
If I am not mistaken, your code in http://www.experts-exchange.com/Programming/Languages/C/Q_28500360.html indicates that you have succeeded in getting some pointer code to compile and run (although not correctly).

ubound, in that other question, gave you some good advice -  
it is VERY bad technique to treat "pointer" and "int" as the same thing.
I think I gave you similar advice here.

There is a lot in a name. So, when I see:
int addressAplusJ;
that just doesn't look right.

Take a good look at your code, rename variables to represent what they are, and review all the comments here and in other questions. You will be able to reduce the number of errors if you do that.

BTW, one convention in naming pointers is to stick "ptr" as a prefix or suffix (or maybe _p as a suffix) so that you know immediately that a variable is a pointer type. A pointer variable holds an address.
0
rgb192Author Commented:
There is a lot in a name. So, when I see:
int addressAplusJ;
that just doesn't look right.

int addressAplusJ = &A + j;

should this be renamed

int ptr_addressAplusJ = &A + j;



I know there is a datatype
int* but that would change the contents of the datatype

Should I use another datatype or are you suggesting to change the variable name
0
phoffricCommented:
My previous post was based on the quote from ubound in http:#a40269440.

>> int ptr_addressAplusJ = &A + j;
Well, the ptr_ prefix is one convention. There are no C-rules for naming a pointer. Unfortunately, on my current project, we do not require any pointer naming indication; but we have prefix rules for other things, so sometimes I just add Ptr as a suffix. (No one complains about that during review.)

>> int* but that would change the contents of the datatype
Post the code where you see changes in the value of the int type vs. the value of the int*. I am interested in seeing how the value would change. Include the two different values in your post.

Since ptr_addressAplusJ is an address of an int, then ptr_addressAplusJ should be a pointer. You should be using int*.

Except for some hardware interfacing, use int* to represent variables that hold addresses.

>> int ptr_addressAplusJ = &A + j;
should be:
int* ptrAplusJ = A + j; // A is an array

Open in new window

An (array-name + integer) represents a pointer to an address &A[j]. (I recall on some compilers long ago, that &A + j gave desired results; but then others complained that on their computers, it gave odd results.)

I recommend reading K&R ANSI C to learn C. It is very easy, succinct, thorough, and most accurate (but it doesn't have C-99 updates which are non-portable to some compilers).
0
rgb192Author Commented:
Post the code where you see changes in the value of the int type vs. the value of the int*. I am interested in seeing how the value would change. Include the two different values in your post.
So
int a=1
and
int *a=1
are the same?
because they are both the value?
0
phoffricCommented:
int a=1;  // a is an integer type
int *b=1; // b is a pointer type (to an int)
In both cases, a and b have a value of 1

You should verify that yourself by looking at their values in the debugger.
0
rgb192Author Commented:
#include <stdio.h>
#include <string.h>

main()
{
	int A[] = { 2, 4, 5, 8, 1 };
	int j;
	int ptr_addressA;
	int valueAj;
	int ptr_addressAplusJ;
	int valueAplusJ;
	for (j = 0; j<5; j++)
	{
		ptr_addressA = &A[j];
		valueAj = A[j];
		ptr_addressAplusJ = &A + j;
		valueAplusJ = *(A + j);
	}
}

Open in new window


okay this runs

I called ptr_ all variables that have a &

I think that these should be the same

            ptr_addressA = &A[j];
            ptr_addressAplusJ = &A + j;


because these are the same
            valueAj = A[j];
            valueAplusJ = *(A + j);
0
phoffricCommented:
Some of the valid relationships were already shown here:
http://www.experts-exchange.com/Programming/Languages/C/Q_28501251.html#a40278125

Seems like same comments are appearing in different questions (which is not permitted).

You made assertions in your previous post. How did they pan out when you ran them?
For each expression in C, you have to identify its type, since C is fairly lax in enforcing type checks. Some of your types are inconsistent.

If you want to learn by doing rather than by reading some recommended books, my recommendation is that you change your file extension from .c to .cpp in Visual Studio to use the C++ compiler which is not so lax and helps prevent mistakes. It will then show you what the incompatible types are. Once you make them compatible, then you can rename the extension back to .c and your program should work better.

You should set up your program to try to find the answer to:
1) the type of &A[0]
2) the type of &A
0
rgb192Author Commented:
// ConsoleApplication2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>
#include <string.h>

int _tmain(int argc, _TCHAR* argv[])
{
	return 0;
}



main()
{
	int A[] = { 2, 4, 5, 8, 1 };
	int j;
	int ptr_addressA;
	int ptr_addressAj;
	int valueAj;
	int ptr_addressAplusJ;
	int valueAplusJ;
	for (j = 0; j<5; j++)
	{
		ptr_addressA = &A;
		ptr_addressAj = &A[j];
		valueAj = A[j];
		ptr_addressAplusJ = &A + j;
		valueAplusJ = *(A + j);
	}
}

Open in new window






Error	1	error C4430: missing type specifier - int assumed. Note: C++ does not support default-int	c:\users\acer\documents\visual studio 2013\projects\consoleapplication2\consoleapplication2\consoleapplication2.cpp	16	1	ConsoleApplication2
Error	2	error C2440: '=' : cannot convert from 'int (*)[5]' to 'int'	c:\users\acer\documents\visual studio 2013\projects\consoleapplication2\consoleapplication2\consoleapplication2.cpp	26	1	ConsoleApplication2
Error	3	error C2440: '=' : cannot convert from 'int *' to 'int'	c:\users\acer\documents\visual studio 2013\projects\consoleapplication2\consoleapplication2\consoleapplication2.cpp	27	1	ConsoleApplication2
Error	4	error C2440: '=' : cannot convert from 'int (*)[5]' to 'int'	c:\users\acer\documents\visual studio 2013\projects\consoleapplication2\consoleapplication2\consoleapplication2.cpp	29	1	ConsoleApplication2
	5	IntelliSense: a value of type "int (*)[5]" cannot be assigned to an entity of type "int"	c:\Users\Acer\Documents\Visual Studio 2013\Projects\ConsoleApplication2\ConsoleApplication2\ConsoleApplication2.cpp	26	16	ConsoleApplication2
	6	IntelliSense: a value of type "int *" cannot be assigned to an entity of type "int"	c:\Users\Acer\Documents\Visual Studio 2013\Projects\ConsoleApplication2\ConsoleApplication2\ConsoleApplication2.cpp	27	17	ConsoleApplication2
	7	IntelliSense: a value of type "int (*)[5]" cannot be assigned to an entity of type "int"	c:\Users\Acer\Documents\Visual Studio 2013\Projects\ConsoleApplication2\ConsoleApplication2\ConsoleApplication2.cpp	29	21	ConsoleApplication2

Open in new window



Running in c++
I now see 'type errors'

so I will do a beginners tutorial to learn what type is.
0
phoffricCommented:
1. For every variable that you set to some value, be sure to use it. That will help identify problem areas. (On my project, we are not permitted to set a variable without using it.)

2. I mentioned earlier as well as others that you should not be using int where you intend to use a variable as a pointer. Change the int types to pointer to int type and that may remove a type error.
      int* ptr_addressA;
      int* ptr_addressAj;
      int* ptr_addressAplusJ;
The above change it just to highlight the point that int types are not pointer types. The above changes are not all correct because some of the types that you use are more complicated.

3. Try putting an int before main() to see if that removes an error.

4. Learning about types in C is important to be able to handle the kinds of code you are trying to tackle now. You are getting into the intermediate area of C programming when you put out code like you have been doing.
0
rgb192Author Commented:
// ConsoleApplication2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>
#include <string.h>

int _tmain(int argc, _TCHAR* argv[])
{
	return 0;
}



int A[] = { 2, 4, 5, 8, 1 };
int j;
int* ptr_addressA;
int* ptr_addressAj;
int valueAj;
int* ptr_addressAplusJ;
int valueAplusJ;
main()
{
	for (j = 0; j<5; j++)
	{
		ptr_addressA = &A;
		ptr_addressAj = &A[j];
		valueAj = A[j];
		ptr_addressAplusJ = &A + j;
		valueAplusJ = *(A + j);
	}
}

Open in new window



Error	1	error C4430: missing type specifier - int assumed. Note: C++ does not support default-int	c:\users\acer\documents\visual studio 2013\projects\consoleapplication2\consoleapplication2\consoleapplication2.cpp	23	1	ConsoleApplication2
Error	2	error C2440: '=' : cannot convert from 'int (*)[5]' to 'int *'	c:\users\acer\documents\visual studio 2013\projects\consoleapplication2\consoleapplication2\consoleapplication2.cpp	26	1	ConsoleApplication2
Error	3	error C2440: '=' : cannot convert from 'int (*)[5]' to 'int *'	c:\users\acer\documents\visual studio 2013\projects\consoleapplication2\consoleapplication2\consoleapplication2.cpp	29	1	ConsoleApplication2
	4	IntelliSense: a value of type "int (*)[5]" cannot be assigned to an entity of type "int *"	c:\Users\Acer\Documents\Visual Studio 2013\Projects\ConsoleApplication2\ConsoleApplication2\ConsoleApplication2.cpp	26	16	ConsoleApplication2
	5	IntelliSense: a value of type "int (*)[5]" cannot be assigned to an entity of type "int *"	c:\Users\Acer\Documents\Visual Studio 2013\Projects\ConsoleApplication2\ConsoleApplication2\ConsoleApplication2.cpp	29	21	ConsoleApplication2

Open in new window



I added *
and declared int and int* before main
0
phoffricCommented:
>> I added *
You should have * when defining pointers - good. Note that I also wrote that "The above changes are not all correct because some of the types that you use are more complicated."

>> so I will do a beginners tutorial to learn what type is.
Then you should be able to answer these questions (except that you probably need to go beyond a beginners tutorial - possibly to the advanced sections):
>> ptr_addressA = &A
What type is &A?
What type is ptr_addressA ?

You have to make the types compatible.

You said that you put an int before the main, you should have reduced the number of errors.
You said that you also put int * before the main - should not have done that.
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
rgb192Author Commented:
I will do a beginners tutorial on type. 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.