Question

How do you do the commented part of this question (its the part that is bolded)?...

How do you do the commented part of this question (its the part that is bolded)?

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

main(){


int *p, in=10; p = &in;
printf("%d\n", *p );

char arr[]="hello";
char *ptr = arr;

// different ways to pass the array, at "pointer" level.
printf("%s %s %s\n", arr, &arr[0], ptr);

//different ways to access arr[0]
printf("%c %c %c %c\n", arr[0], *ptr, *arr, ptr[0]);

//different ways to access arr[1]
printf("%c %c %c %c\n", arr[1], *(ptr+1), *(arr+1), ptr[1] );

//different ways to access arr[4]
printf("%c %c %c %c\n", arr[4], *(ptr+4), *(arr+4), ptr[4] );


/****simple array of int pointers **********************************/

int i=1; int j=3; int k; int n;
int * x[3]; // array of 3 int pointers
x[0]= &i;
x[1]= &j;
x[2]= &k;

// set k to 5 via its pointer

n=0;
for (; n<3; n++)
;// printf("%d %d", , ) // print elements of array x; using both array notation and pointer notation. Should print 1 1 ,3 3, 5 5

/***** array of char pointers, each pointer points to an char array *********************************/

char * planets[] = {"Mercury", "Venus", "Earth",
"Mars", "Jupiter", "Saturn",
"Uranus", "Neptune", "Pluto"};

// print the pointee of the 1st element pointer (i.e., string "Mercury") in the pointer array, using both array index notation i.e., planets[i] and pointer notation,

// print the pointee of the 2nd element pointer (i.e., string "Venus"), using both array index notation and pointer notation,

// print the pointee of the 5th element pointer (i.e., string "Jupiter"), using both array index notation and pointer notation,

// print the pointee of the 6th element pointer (i.e., string "Saturn"), using both array index notation and pointer notation,

// print the pointee of the 8th element pointer (i.e., string "Neptune"), using both array index notation and pointer notation,

// declare a pointer pp (what type??) to point to the first element of planets;

// print the pointee of the 1st element pointer (i.e., string "Mercury") in the pointer array, via pp and using pointer notation,

// print the pointee of the 2nd element pointer (i.e., string "Venus"), via pp and using pointer notation,

// print the pointee of the 5th element pointer (i.e., string "Jupiter"), via pp and using pointer notation,

// print the pointee of the 6th element pointer (i.e., string "Saturn"), via pp and using pointer notation,

// print the pointee of the 8th element pointer (i.e., string "Neptune"), via pp and using pointer notation,

/* access the characters in the pointee strings. */
printf("%c %c %c %c\n", *( *(planets+4) +3 ), planets[4][3], *( *(pp+4) +3 ), pp[4][3] );
printf("%c %c %c %c\n", **planets, planets[0][0], **pp, pp[0][0] ); // *( *(planets+0) +0 ),
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

COMPLETED C CODE

#include <stdio.h>

#include <string.h>

main(){

int *p, in=10; p = &in;

printf("%d\n", *p );

char arr[]="hello";

char *ptr = arr;

// different ways to pass the array, at "pointer" level.

printf("%s %s %s\n", arr, &arr[0], ptr);

//different ways to access arr[0]

printf("%c %c %c %c\n", arr[0], *ptr, *arr, ptr[0]);

//different ways to access arr[1]

printf("%c %c %c %c\n", arr[1], *(ptr+1), *(arr+1), ptr[1] );

//different ways to access arr[4]

printf("%c %c %c %c\n", arr[4], *(ptr+4), *(arr+4), ptr[4] );

/****simple array of int pointers **********************************/

int i=1; int j=3; int k; int n;

int * x[3]; // array of 3 int pointers

x[0]= &i;

x[1]= &j;

x[2]= &k;

// set k to 5 via its pointer

n=0;

for (; n<3; n++)

;// printf("%d %d", , ) // print elements of array x; using both array notation and pointer notation. Should print 1 1 ,3 3, 5 5

/***** array of char pointers, each pointer points to an char array *********************************/

char * planets[] = {"Mercury", "Venus", "Earth",

"Mars", "Jupiter", "Saturn",

"Uranus", "Neptune", "Pluto"};

// print the pointee of the 1st element pointer (i.e., string "Mercury") in the pointer array, using both array index notation i.e., planets[i] and pointer notation,

printf("\nPrinting 1st element:\n");

printf("Printing using array index: %s\n", planets[0]);

printf("Printing using pointer notation: %s\n", *(planets + 0));

// print the pointee of the 2nd element pointer (i.e., string "Venus"), using both array index notation and pointer notation,

printf("\nPrinting 2nd element:\n");

printf("Printing using array index: %s\n", planets[1]);

printf("Printing using pointer notation: %s\n", *(planets + 1));

// print the pointee of the 5th element pointer (i.e., string "Jupiter"), using both array index notation and pointer notation,

printf("\nPrinting 5th element:\n");

printf("Printing using array index: %s\n", planets[4]);

printf("Printing using pointer notation: %s\n", *(planets + 4));

// print the pointee of the 6th element pointer (i.e., string "Saturn"), using both array index notation and pointer notation,

printf("\nPrinting 6th element:\n");

printf("Printing using array index: %s\n", planets[5]);

printf("Printing using pointer notation: %s\n", *(planets + 5));

// print the pointee of the 8th element pointer (i.e., string "Neptune"), using both array index notation and pointer notation,

printf("\nPrinting 8th element:\n");

printf("Printing using array index: %s\n", planets[7]);

printf("Printing using pointer notation: %s\n", *(planets + 7));

// declare a pointer pp (what type??) to point to the first element of planets;

char **pp;

pp = planets;

pp = &planets[0];

// print the pointee of the 1st element pointer (i.e., string "Mercury") in the pointer array, via pp and using pointer notation,

printf("\nPrinting 1st element:\n");

printf("Printing via pp: %s\n", pp[0]);

printf("Printing using pointer notation: %s\n", *(pp + 0));

// print the pointee of the 2nd element pointer (i.e., string "Venus"), via pp and using pointer notation,

printf("\nPrinting 2nd element:\n");

printf("Printing via pp: %s\n", pp[1]);

printf("Printing using pointer notation: %s\n", *(pp + 1));

// print the pointee of the 5th element pointer (i.e., string "Jupiter"), via pp and using pointer notation,

printf("\nPrinting 5th element:\n");

printf("Printing via pp: %s\n", pp[4]);

printf("Printing using pointer notation: %s\n", *(pp + 4));

// print the pointee of the 6th element pointer (i.e., string "Saturn"), via pp and using pointer notation,

printf("\nPrinting 6th element:\n");

printf("Printing via pp: %s\n", pp[5]);

printf("Printing using pointer notation: %s\n", *(pp + 5));

// print the pointee of the 8th element pointer (i.e., string "Neptune"), via pp and using pointer notation,

printf("\nPrinting 8th element:\n");

printf("Printing via pp: %s\n", pp[7]);

printf("Printing using pointer notation: %s\n", *(pp + 7));

/* access the characters in the pointee strings. */

printf("%c %c %c %c\n", *( *(planets+4) +3 ), planets[4][3], *( *(pp+4) +3 ), pp[4][3] );

printf("%c %c %c %c\n", **planets, planets[0][0], **pp, pp[0][0] ); // *( *(planets+0) +0 ),

}

OUTPUT

E\Program Files Cplusplus workspace projectile.exe RANGE OF A PROJECTILE Enter the launch angle in degrees 30 Enter the launc

Add a comment
Know the answer?
Add Answer to:
How do you do the commented part of this question (its the part that is bolded)?...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 2. Pointer arithmetic: a) Write a printString function that prints a string (char-by-char) using pointer arithmetics...

    2. Pointer arithmetic: a) Write a printString function that prints a string (char-by-char) using pointer arithmetics and dereferencing. b) Write a function “void computeMinMax(double arr[], int length, double * min, double * max)” that finds the minimum and the maximum values in an array and stores the result in min and max. You are only allowed to use a single for loop in the function (no while loops). Find both the min and the max using the same for loop....

  • Please use Visual Studio! Let me know if you need anything else <3 #include <stdio.h> #include...

    Please use Visual Studio! Let me know if you need anything else <3 #include <stdio.h> #include <string.h> #pragma warning(disable : 4996) // compiler directive for Visual Studio only // Read before you start: // You are given a partially complete program. Complete the functions in order for this program to work successfully. // All instructions are given above the required functions, please read them and follow them carefully. // You shoud not modify the function return types or parameters. //...

  • 1. You are given a C file which contains a partially completed program. Follow the instructions...

    1. You are given a C file which contains a partially completed program. Follow the instructions contained in comments and complete the required functions. You will be rewriting four functions from HW03 (initializeStrings, printStrings, encryptStrings, decryptStrings) using only pointer operations instead of using array operations. In addition to this, you will be writing two new functions (printReversedString, isValidPassword). You should not be using any array operations in any of functions for this assignment. You may use only the strlen() function...

  • If void * is a pointer to void is a "generic" pointer type, and a void...

    If void * is a pointer to void is a "generic" pointer type, and a void * can be converted to any other pointer type * without an explicit cast, why in the ,myarrcopy function the malloc is created like char and not like void? if we are going to work with different type of arrays? Here is de program: *This is a function that make a copy of any given array. * We then use this function make a...

  • So i need help with this program, This program is supposed to ask the user to...

    So i need help with this program, This program is supposed to ask the user to input their weight on earth so I would put "140" then it would ask "enter planet name, Min, Max or all" and if the user typed in all it would display all the planets and your weight on them. Right now Im suck on the part where im required to do a switch statement with a linear search. Below this im going to put...

  • I am trying to write the following code using pointers and traversal by pointers instead of...

    I am trying to write the following code using pointers and traversal by pointers instead of indexing (in C++). The following code is correct: void shift(int arr[], int n) { int temp = arr[n - 1]; int temp1; for (int i = 0; i < n; i++) { temp1 = arr[i]; arr[i] = temp; temp = temp1; } } This is my ATTEMPT at writing it with pointers (and traversal by pointers!) but I know it is wrong. I believe...

  • Need help in C (a) Write a C program to read in a line of text...

    Need help in C (a) Write a C program to read in a line of text and count the occurrence of each English alphabet. The lowercase version of a letter is considered the same as the uppercase. To make viewing easy, the frequencies should be presented using a bar chart as follows. You can assume that the input contains only spaces, lowercase letters, uppercase letters and the newline character (i.e. the Enter key). Enter a line of text: Letter ZZz...

  • Please answer problem #5 thank you str.c #include "str.h" #include <stdio.h> int str_len(...

    Please answer problem #5 thank you str.c #include "str.h" #include <stdio.h> int str_len(char *s) {    /* this is here so code compiles */ return 0; } /* array version */ /* concantenate t to the end of s; s must be big enough */ void str_cat(char s[], char t[]) {    int i, j;    i = j = 0;    while (s[i] != '\0')    /* find end of s */        i++;    while ((s[i++] = t[j++]) != '\0') /* copy t */        ;...

  • How can I convert the following C code to MIPS Assembly? +++++++++++++++++++++++++++++++++ MIPS main program ++++++++++++++++++++++++++++++++...

    How can I convert the following C code to MIPS Assembly? +++++++++++++++++++++++++++++++++ MIPS main program ++++++++++++++++++++++++++++++++ .data # Defines variable section of an assembly routine. array: .word x, x, x, x, x, x, x, x, x, x # Define a variable named array as a word (integer) array # with 10 unsorted integer numbers of your own. # After your program has run, the integers in this array # should be sorted. .text # Defines the start of the code...

  • c++ please read all question edit the program to test different random sizes of the array and give me the time in a file will be like random size of the array and next to it the time it took for each...

    c++ please read all question edit the program to test different random sizes of the array and give me the time in a file will be like random size of the array and next to it the time it took for each size Im trying to do time analysis for Quick sort but i keep getting time = 0 also i want edit the program to test different random sizes of the array and give me the time in a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT