0
Pointers
int main() { char **trip = { "suitcase", "passport", "ticket" }; printf("Please bring the following:\n"); for (int i = 0; i < 3; i++) { printf("%s\n", trip);//here why we can t access all words } return 0; } what we can do to access all the elements
3 Answers
+ 3
"I want to use **trip"
#include <stdio.h>
#include <stdlib.h>
int main()
{
char **trip = malloc(3 * sizeof(char *)); // Dynamic allocation for 3 pointer to char
trip[0] = "suitcase";
trip[1] = "passport";
trip[2] = "ticket";
printf("Please bring the following:\n");
for (int i = 0; i < 3; i++) {
printf("%s\n", *(&trip[0] + i) ); // dereference the base address of each string
}
free(trip); // Free up the memory
return 0;
}
+ 5
#include <stdio.h>
int main()
{
char *trip[3] = { // char **trip --> char *trip[3] -- trip is a pointer of 3 chars
"suitcase",
"passport",
"ticket"
};
printf("Please bring the following:\n");
for (int i = 0; i < 3; i++) {
printf("%s\n", trip[i]); // trip --> trip[i]
}
return 0;
}
0
thanks you .but i want to use **trip not *trip[3] .