?
Solved

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

Posted on 2014-08-16
19
Medium Priority
?
279 Views
Last Modified: 2014-09-14
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

0
Comment
Question by:rgb192
  • 10
  • 9
19 Comments
 
LVL 32

Expert Comment

by:phoffric
ID: 40265178
Is this homework?
0
 
LVL 32

Expert Comment

by:phoffric
ID: 40265179
addressA and j are int.
addressA[j] makes no sense since neither j nor addressA is an array.
0
 

Author Comment

by:rgb192
ID: 40265550
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 32

Expert Comment

by:phoffric
ID: 40265561
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
 

Author Comment

by:rgb192
ID: 40266840
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
 
LVL 32

Expert Comment

by:phoffric
ID: 40266921
int * addressA = &A[j];
addressA  is now a pointer variable that is pointing to an int.

Use %x in your printf for addressA .
0
 

Author Comment

by:rgb192
ID: 40268908
#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
 
LVL 32

Expert Comment

by:phoffric
ID: 40269440
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
 

Author Comment

by:rgb192
ID: 40277786
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
 
LVL 32

Expert Comment

by:phoffric
ID: 40277919
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
 

Author Comment

by:rgb192
ID: 40295619
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
 
LVL 32

Expert Comment

by:phoffric
ID: 40295652
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
 

Author Comment

by:rgb192
ID: 40307678
#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
 
LVL 32

Expert Comment

by:phoffric
ID: 40307958
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
 

Author Comment

by:rgb192
ID: 40308126
// 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
 
LVL 32

Expert Comment

by:phoffric
ID: 40308236
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
 

Author Comment

by:rgb192
ID: 40310063
// 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
 
LVL 32

Accepted Solution

by:
phoffric earned 2000 total points
ID: 40310317
>> 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
 

Author Closing Comment

by:rgb192
ID: 40321808
I will do a beginners tutorial on type. Thanks.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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 recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses

839 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