This program output should be 11 [closed] - c

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
#include<stdio.h>
#include<conio.h>
void main()
{
int *(intArray+3);
int i;
int (intArray+3)=&i;
*(intArray+3)=11;
printf("%d%d",i,*(intArray+3));
getch();
in this program i used the concept *(intArray+3)=intArray[3]
with base program
int *p;
int i;
p=&i;
*p=11;
printf("%d%d",i,*p);
in this case o/p is 11,11 but 1st program is based on this concept plez help?

The pointer arithmetic is possible only after defining and initializing a pointer. Now let me explain it:
Suppose there an array intArray of 5 elements ;
int intArray[5];
Let's assume the starting address of intArray is 2000. Now what (intArray + 3) does mean here? It means that (intArray + 3) is now referring to the fourth element (element 3) of the array intArray, i.e, now it is pointing to the location 2012 (as array name can be used as a pointer to its first element), assuming an int type is taking 4 bytes on a machine. When you place a * operator before it then it dereferences the value at that location, i.e, *(intArray + 3) will give you the value stored at the location 2012 which is equivalent to intArray[3].
Since you declared intArray neither as a pointer (for pointer it must be initialized) nor as an array, you can't dereference (intArray + 3). This is invalid and program will not compile.

Related

How can *ptr be used with (.) and (&test) can be used with -> in following code? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have written following program .
#include<stdio.h>
#include<stdlib.h>
struct Test
{
int count;
};
void main()
{
struct Test *ptr;
struct Test test;
ptr = malloc(sizeof(struct Test));
if ( ptr)
{
ptr->count = 123;
test.count = 456;
printf("count : %d ",(*ptr).count);
printf("count : %d ",(&test)->count);
}
else
{
printf(" malloc failed \n");
}
}
output of the program:
count : 123
count : 456
Can anyone please explain how to represent (*ptr).count and (&test)->count in this code and how it is working ?
So ptr and test are 2 vars of different types. ptr is a pointer to struct Test, whereas test just is that struct. As per usual, you access values of pointers through indirection (denoted by -> as opposed to .).
To reassign a pointer, first you have to dereference the pointer, which is done using the * operator, hence: (*ptr).count is basically read as: get the value ptr points to and use it. The entire expression is basically the long-form of ptr->count.
(&test)->count on the other hand is a bit silly. You have an object on stack, but rather than accessing its fields quickly and cheaply, you are getting the memory address (which is what & does - it's the "address of" operator), and creating an in-place pointer. Then you are getting the value stored in count using indirection. The expression (&test)->count reads as: "get me a pointer to test, and using that pointer, access the field count". It's equivalent to *(&test).count A bit redundant, then.
(&test)->count is equivalent to (*(&test)).count. For more, you can glance at Arrow operator (->) usage in C
Well, a->x is basically short for (*a).x, it's just that working with pointers to structures is quite common in C, so the syntax has some extra sugar there to make it simpler. Dropping the need for parentheses is really helpful in my opinion.
Basically you use . when the value on the left-hand side is of struct type, and -> when it is the address of a structure.

Print data of 2 variables using pointer in code below [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 6 years ago.
Improve this question
I have the following code:
#include <stdio.h>
#include <stdlib.h>
int *p;
int main() {
int a = 4, b = 8;
p = &b;
//TODO: fill in the blank
printf("a = %ld\n", /*Fill in here */);
printf("b = %ld\n", /*Fill in here */);
return 0;
}
How can I print the value of 2 variables a and b using only p to access them?
In general, you cannot do this with current code /approach.
There's nothing in C standard that guarantees the memory allocation strategy for two or more independent variables, so the previous-next memory location tracking is not possible. There's no deterministic way to deduce the value of a, with only access to p.
In case you need to access values of more than one variables (of same type) from a single pointer, consider creating an array, where the elements are guaranteed to reside in contiguous memory location, so pointer arithmetic is meaningful and we can reach the previous-next element deterministically.

Pointers in C with arrays [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Here is the code:
#include <stdio.h>
int main ()
{
int c[4][4] = {{0,1,2,3},{4,5,6,7},{8,9,10,11},{12,13,14,15}};
int (*p)[3] = (int(*)[3])(c+2);
return 0;
}
Its interesting that it sets p to be:
{{8,9,10},{11,12,13},{14,15,-8224}}
I do not understand what is happening, especially in the 6th line of the code. Please help me!
int c[4][4] declares a 2D array with dimensions 4x4.
int (*p)[3] declares an array pointer to an array of 3 elements.
(int(*)[3])(c+2) invokes a pointer aliasing bug, by treating the address of c+2 as if there was an array of 3 elements there.
Pointer conversions between type int (*)[4] (the type of c+2) and type int(*)[3] are not safe. In practice, most compilers will likely give you some deterministic result from this code, but they are not required to do so. The program may as well crash and burn and then that's the programmer's fault.

typecasting a pointer to a multidimensional array in C [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
# include <stdio.h>
# include <stdlib.h>
int main(int argc, char *argv[])
{
int a[5][10][2];
int *p;
p = (int(*)[10][2])p;//Gives error!
return EXIT_SUCCESS;
}
I want to type cast p to type so that it can act as a pointer to the given 3-d array?Is there a way to do so.I am applyindg the idea that the type of a variable is everything except variable name.
Why are you trying to "typecast" anything? Why would you expect a value "typecasted" to (int(*)[10][2]) to be compatible with an int * pointer? And why does your original code assigns p to p, completely ignoring a?
This is what you can do
int a[5][10][2];
int (*p)[10][2] = a;
Now p is a pointer that can be used to access a, i.e. p[i][j][k] is equivalent to a[i][j][k]. No typecasting necessary.
If you write p = (int(*)[10][2])a; it won't give you any errors, may be a warning. You are thinking that p will be converted to pointer to a 3-D array, which is wrong. Try this statement after assigning a to p.
printf("addresses %u %u",p,p+1);
According to you, output should be something similar to this(lets say) "addresses 9990000 99940000", because you are thinking p is pointing to 3-D array. However you will get similar to "addresses 9990000 9990004", proving that p is a pointer to an integer.

issue with length macro [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I have a simple macro:
#define LENGTH(arr) (sizeof(arr)/sizeof(arr[0]))
and for whatever reason, it seems to work fine when setting something like:
int length = LENGTH(arr)
but not to compare with in a loop
while(i < LENGTH(arr))
For the same arr, the macro will either work or it won't.
The likely problem is that the arr in the loop is a pointer rather than an array. The macro does not work with pointers, it only works with arrays.
Remember that arrays decays to pointer when passed around. So if you pass an array to a function, it's not longer an array but a pointer inside the function. A pointer which have only have information about the type, but not the size of the array.
sizeof(arr) is not the same as what a more high level language gives you when you do arr.Count() or arr.Length.
This gives you the storage space of the arr variable. This does not mean the length of the array.
If you have char *arr = malloc(sizeof(*arr) * 100); and you do sizeof(arr) you will usually (depending on your system) get 4. If you do sizeof(*arr) or sizeof(arr[0]), which is the same thing, you have sizeof(char) which is 1, so the macro gives you 4/1 = 4 : wrong. It works with char arr[100] because sizeof(arr) gives you 100 as the array does not decay to a pointer type for sizeof.

Resources