My background is C#, I have the following code(in C) that I need to tell what would be the output:
#include <stdio.h>
#define N 10
int main()
{
int a[N] = { 3,4 }; //<-- [3][4][0][0][0][0][0][0][0][0]
int *q = a; //<-- some address
char s[N] = "abcdefg";
int k = s[*a] - s[a[*q]]; //<-- what is that mean ?
char *p = s;
a[k] = k;
printf("%d %s\n",k,p+a[k]);
printf("%d %d %d %d %d",q[0],q[1],q[2],q[3],q[4]);
return 0;
}
Can someone please explain about line number 4?
I can't understand s[*a] is that mean s at index of *a? but *a is address not an integer. same question about s[a[*q]]
Considering
int k = s[*a] - s[a[*q]];
First *a gets the value at a[0] which is 3.
We know that q == a so *q is the same as *a which again is the value 3. The value at a[3] is 0.
So now we have s[3] - s[0] which is 'd' - 'a' which is 3 (assuming ASCII encoding), the value printed out for k.
Without the rest of the code, it is unclear if it will work at all. What is N, for example?
Specifically, s[*a]: a is the name of an array. *a is the same as a[0], because a is a pointer to the first element. So *a is 3, and s[*a] is s[3], which is the character 'd'
Related
Edit. Sorry for the minimal information included previously
Say I have the following code:
char ** a[16];
a[15] = '\0';
int i;
for (i = 0; i < 5; i++) {
char * b[3];
b[2] = '\0';
b[0] = "foo";
b[1] = "bar";
if (i == 4) {
b[0] = "hello";
b[1] = "world";
}
a[i] = b;
}
Straight after the for loop, if I include the following two lines:
printf("%s %s\n", a[0][0], a[0][1]);
printf("%s %s\n", a[4][0], a[4][1]);
I want the output to be:
foo bar
hello world
However it is instead:
hello world
hello world
I am aware this is because of my declaration, a[i] = b;, where b is an array of char pointers. Each loop, the character pointers pointed to by b[0] and b[1] are changed.
In the final loop they are set to "hello" and "world" respectively. Since I assigned b to a[i], every index of a now points to the same thing.
What I would like to do is dereference b, such that a[i] is given the value b points to rather than b itself. Therefore after the loop all indexes of a are not the same.
I tried using the following but both resulted in segmentation faults:
*a[i] = *b
and
**a[i] = **b
Any help would be much appreciated, as I'm totally lost. Thank you :)
What I would like to do is dereference b, such that a[i] is given the value b points to rather than b itself. Therefore after the loop all indexes of a are not the same.
Note that b itself is an array, so you cannot just assign value b points to rather than b itself because b does not point to a single value.
To really do this (not sure why you would want this) replace the line:
a[i] = b;
a[i][0] = b[0];
a[i][1] = b[1];
a[i][2] = b[2];
I'm looking for the expalanation of this problem. I cannot understand the while part and why does it print 6.
#include <stdio.h>
#include<stdlib.h>
int main()
{
int array[] = {1, 2, 4, 0, 4, 0, 3};
int *p, sum = 0;
p = &array[0];
while (*p++)
sum += *p;
printf("%d\n", sum);
return 0;
}
This is a more readable form of your while loop:
while (*p != 0)
{
p = p + 1; // you can put "p++;" here, which has the same effect
sum += *p;
}
Now you should understand on your own.
Initially the pointer p points to the first element of the array.
p = &array[0];
However after evaluation the condition of the while statement
while(*p++)
the pointer was incremented and after that it points to the second element of the array that is to 2.
Within the body of the loop there is used the incremented pointer.
So you will have
2 + 4
The forth element of the array is equal to 0. So the loop stops its iterations.
You can consider the condition in the while loop the following way if to rewrite it as a for loop
#include <stdio.h>
int main(void)
{
int array[] = {1,2,4,0,4,0,3};
int *p, sum=0;
p = &array[0];
for ( int *tmp = p; ( ++p, *tmp != 0 ); tmp = p )
{
sum += *p;
}
printf("%d\n",sum);
return 0;
}
If within the body to use the statement
sum += *tmp;
instead of
sum += *p;
then you will get the expected result equal to 7.
you are discarding the value from first element of array.
that is the reason why you see answer 6
you sum elements after you already moved to the next element of array
you should increment pointer in the end of iteration
while(*p){
sum += *p;
p++;
}
or
for( ; *p ; p++){
sum += *p;
}
The loop runs until it encounters a zero value. However you increment your pointer before adding what it points to, to "sum".
So the loop adds the second and third elements to sum and stops at the fourth.
The value of sum is then: 0 + 2 + 4 = 6
Just modify your while loop as
while(*p)
sum += *p++;
Explanation
++, –– are unary operators used to increment, decrement the value of a variable by 1.
They can be used as postfix or prefix
But this was not just your problem. While loop terminated when it receives 0. Your array contains one of it.
So, instead use
for(int i=0;i<7;i++) //7 -> size of array
sum += *p++;
o/p - 14
Can all the pointer members of an array point to the same variable?
ex: What happens when *a[i] is pointed to a variable C of value 5 and the length of the array is 3? Will a[0],a[1],a[2] have the values 5?
Yes, based on this answer
If all pointers are of the same type, there is no issue
int a = 10;
int *b[3];
b[0] = &a;
b[1] = &a;
b[2] = &a;
a = 5; // now b[0-2] are also equal 5.
No a[0],a[1],a[2] will not point to variable C, you have to explicitly make them point to variable C. Consider below example.
#include<stdio.h>
void main()
{
int *a[3];
int c=5;
a[0] = &c;
printf("%d %d %d", *a[0],*a[1],*a[2]);//this will print 5 junkvalue junkvalue
a[1] = &c;
a[2] = &c;
printf("%d %d %d", *a[0],*a[1],*a[2]);//this will print 5 5 5
}
I have problem with my own function for get array length.
Code:
#include <stdio.h>
int main() {
int a[] = {5,4,1,2,1}; //len -> 6!! FAIL! WHY?
//int a[] = {5,4,1,2}; //len -> 4 OK!
int len = 0;
int *p = a;
while(*p != '\0'){
printf("%d\n", *p);
len++;
*p++;
}
printf("len: %d\n", len);
return 0;
}
code above output:
5
4
1
2
1
32767
len: 6
but this array int a[] = {5,4,1,2}; - produce len = 4 - ok.
why it happens?
It fails because there's no 0 at the end of your array. It's not added automatically, you need to do it explicitly. As a result, you accessed outside the array, which results in undefined behavior. The fact that one of your tests seemed to work was purely luck, you can't depend on it.
int a[] = {5, 4, 1, 2, 1, 0};
The only time that C automatically adds a null terminator is when you use a string literal to initialize a char array, e.g.
char c[] = "abcde";
I'm very new to dealing with pointers, and my C knowledge is fairly small. I'm trying to understand pointers. I wrote the following code to print a list of variables (a to f) like so:
0
1
2
3
4
5
I wrote the following code to do this:
#include <stdio.h>
int main(){
int a,b,c,d,e,f;
int *p;
int i;
a = b = c = d = f = 0;
p = &a;
for (i = 0; i < 5; i++){
*p += i;
printf("%d\n", *p);
p++;
}
return 0;
}
The idea was it works through the variables and increments each by an ever-increasing number (i). I am assuming that as you initialize the variables at the same time, they'd be placed next to each other in memory. However, I get the following output:
0
1
2
3
-1218283607
If I change the for loop to only go from 0 to 3 (i < 4), it works fine, printer 0 1 2 and 3. But when I wish to print the variable f as well, it doesn't seem to set it.
As I said, I'm very new to pointers so I've probably overlooked something silly, but I've been looking through my code over and over, trying to work it out.
Thanks in advance.
There is no guarantee that a, b, c, d, e and f will be adjacent in memory. If you want that sort of guarantee you need to use an array.
#include <stdio.h>
int main() {
int a[6];
int *p;
int i;
a[0] = a[1] = a[2] = a[3] = a[4] = a[5] = 0;
p = &a[0];
for (i = 0; i < 6; i++){
*p += i;
p++;
}
for(i = 0; i < 6; i++) {
printf("%d\n", a[i]);
}
return 0;
}
Here int a[6] is declaring an array named a that can hold six integers. These six integers can obtained via a[0], a[1], a[2], a[3], a[4] and a[5]. You are guaranteed that a[0], a[1], a[2], a[3], a[4] and a[5] are layed out contiguously in memory. Thus the line
p = &a[0];
sets p to the address of the first element. Each increment of this pointer moves us forward one position in the array.
The second for loop shows that first for loops correctly sets a[i] to i for i in {0, 1, 2, 3, 4, 5}. If you run this program you will see
0
1
2
3
4
5
on the console.
You forgot to initialize e. But yes, use a packed array.
It isn't safe to assume that stack variables are arranged in memory in any particular order.
You need to use an array, a struct or possibly a union to gurantee the ordering of your ints.
union {
int ary[6];
struct {
int a;
int b;
int c;
int d;
int e;
int f;
} s;
} u = {0};
p = &u.s.a;
for (i = 0; i < 5; i++){
*p += i;
printf("%d\n", *p);
p++;
}