C Practice Test Ints and Char Arrays [closed] - c

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I was asked this on a practice test, that has no answers posted. I have no way to test the code, but it has confused me. Can you please help me out with not only understanding the answer, but why.
int foo() {
int a = 1;
char b[] = "zapples";
a = *(b + 1);
if (a == 'a') return 1;
else return 0;
}
What does foo return? ____

Have they taught you how C pointers work?
I'm not going to give you a straight answer, but think about this:
b has the starting address of string "zapples". This means that b[0] points to "z". Another notation for this is *(b + 0), that is, "the value contained at address b, with an offset of 0). With this information, what is the value of *(b + 1)?
This should be enough to solve the exercise.

It will return 1.
The line a = *(b + 1) is the important one. It takes b as a pointer to the first element in the array and adds one so it points to the second. It is then dereferenced so that the value at that address 'a' is assigned to the variable a.

Related

how can I covert int to char in C [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I'm trying to covert an int to char. Is there any way to do that?
For example:
{
int i;
char d;
i = 55;
d = i;
printf("%c\n", d);
}
How do I make d = 55?
If you want to put the number 55 into a string, use sprintf
Indeed your example can do what you want.
If you really want to place safe, you may:
d = (char) i;
Try this code segment:
printf("%d\n", d);
char are presented in the memory as binary format wich is equivalent to a number and this number is called a code ascii. when you print the code ascii with "%c" Then it will print the charchter equivalent to this code ascii

Why is this simple code not compiling? [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I have problem to compile the following lines:
/*This code compiles with error*/
char HeLev1[6];
HeLev1[]="45.0";
/*but this is OK:*/
char HeLev1[6]="45.0";
You cannot assign values to an array. You need to assign values to array elements one-by-one (or, when dealing with strings, using strcpy())
char HeLev1[6];
strcpy(HeLev1, "45.0");
char HeLev2[6];
HeLev2[0] = '4';
HeLev2[1] = '5';
HeLev2[2] = '.';
HeLev2[3] = '0';
HeLev2[4] = '\0'; /* properly "terminate" the string */
Note that in your code, the OK part, you have an array initialization, not assignment.
Also note that, in both cases above, the 6th element (HeLev1[5] or HeLev2[5]) has an undefined value (garbage).
you can assign whole values to an array only while initialization. like these are correct forms,
char HeLev1[6]="45.0";
int array[3]={1,2,3};
char HeLev1[]="45.0";
int array[]={1,2,3};
but once you have skipped this part. you have to assign element by element. like,
char HeLev2[6];
HeLev2[0] = '4';
HeLev2[1] = '5';
HeLev2[2] = '.';
HeLev2[3] = '0';
HeLev2[4] = '\0'; /* properly "terminate" the string */
or you can use memcpy or strcpy.

C initialise 3D array with values [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I'm trying to initialise all elements of a 3D array "A". The array consists of 2000x100x4 integer elements of the 3D array and is stored in row-major order. Each index at position [i,j,k] in "A" must be initialised with the value i*i*i + j*j*j.
How can I do this using for loops? Any suggestions? Thanks.
for(i=0;i<2000;i++)
for(j=0;j<100;j++)
for(k=0;k<4;k++)
A[i][j][k]= (i*i*i) + (j*j*j);
I hope I understood your question correctly. Or were you looking for something else?
It's not something hard to do:
int A[2000][100][4];
int i,j,k;
for (i=0;<2000;i++)
{
for (j=0;j<100;j++)
{
for (k=0;k<4;k++)
{
A[i][j][k] = i*i*i + j*j*j;
}
}
}

'strcmp' char array in C [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
...
main() {
char **src_ip[10];
char **dest_ip[10];
char **lat[10];
char *ip[20];
while{
//Read file
//fgets();
src_ip[j] = &data[0];
dest_ip[j] = &data[1];
lat[j] = &data[2];
int idx;
int addip;
for(idx=0; idx<20; idx++)
{
addip = 0;
//Check to see if the IP address is already in the array.
if ((strcmp(*(src_ip[j]), ip[idx]) == 0) ||
(strcmp(*(dest_ip[j]), ip[idx]) == 0))
{
addip=1;
break;
}
//If the IP address was already found then addip would equal 1.
if (!addip){
printf("new node: %s",*(ip[idx]));
}
else
printf("Exist");
}
}
j++;
}
I want to compare char string in two 1d array - array src_ip[j] and dest_ip[j] - and insert into one array ip[idx]. For example, when I look for ip[1], it will go to src_ip[1], meaning that it's only referring to one array - ip[idx] - instead of looking separately into src_ip and dest_ip.
There must be a problem in the code - maybe I left something out?
if((strcmp(*(src_ip[j],ip[idx]) == 0) && (strcmp(*(dest_ip[j]),ip[idx]) == 0))
^^
You can't have both *(src_ip[j]) and *(dest_ip[j]) equal to ip[idx]. What you meant to use was the || operator instead of &&. (By the way, there's a missing ) where I marked above)
Also your while(idx) doesn't make sense. Are you controlling the loop with the for above, or the while below? If with the while, then you'll get an infinite loop if the condition is true since you are never changing it inside the loop.

finding out modified element in an array using XOR property [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
This question is being attempted from here
How to find out modified element in an array?
Eg: Array A= {1,2,3,4,5,6} before modifiction After modification
A={1,2,3,7,5,6}. Here the element 4 is replaced with element 7.
Some one used a XOR Property to solve this problem
int getModifiedElement(int arr1[ ],int arr2[ ])
{
int xor1 = arr1[0];
int xor2 = arr2[0];
for(int i=1;<i<arr1.length;i++)
{
xor1 = xor1 ^ arr1[i];
xor2 = xor2 ^ arr2[i];
}
return xor1^xor2;
}
I am not clear with what XOR property has been used to solve the problem?
xor is associative and commutative, so when xor1 xors together all elements from array 1 and xor2 those from array 2, the unmodified elements are present in both, so with xor1 ^ xor2, they're eliminated because x ^ x == 0, so what remains is old ^ new in xor1 ^ xor2, that is the modification leading from the old to the changed element of the array.
It does, however, neither say what the old element was, nor what the new is, so it does not solve the problem as stated.

Resources