How can I construct the array problem?

Pointers and pointers array problem?

  • The point is I was implementing a shell in linux (Operating Systems lab) using execvp which taker an array of character pointers as an arguement. The first arguement is the command and then input the whole array. Notice that the first element of the array must be the command. That's how execvp works and that's not my question. From What I've learned about C, and C++ about pointers... When I do perform char * array [size] ; I expect every pointer of the array to point to a single character ..... which contradict the arguements of execvp as it takes an array of pointers that points to a word or a string. How could that be done? Thats a sample of how execvp works though I dont usderstand the initialization of char pointer array execArgs. int main(void) { char *execArgs[] = { "echo", "Hello, World!", NULL }; execvp("echo", execArgs); return 0; } My question is how can I initialize a character pointer array as mentioned .... Isn't that a string array? as when I address execArgs[0] it gives me echo and I expect e ( a single char ) .... I want to understand how it works? An how can I use something like that if I have an array of strings in order to make a character pointer array for the execvp function. ( I can't initialize them the same way)

  • Answer:

    I guess people who are used to languages where there is an explicit string type get a little confused when working with C. There's no difference between a pointer to a string and a pointer to a char in C. It's the caller and callee agreeing that the char that is pointed to is actually the first character of a null-terminated sequence of characters that makes the difference. So yes, you're correct, each pointer in the array points to a single char. However, that char is the first character in a sequence of characters, terminated by a null character (zero byte) that forms a C string.

Shehab at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

Normally, at the shell prompt we can use echo command like this: echo Hello World Here, echo is the command name and Hello World is its argument. When we do so, shell uses PATH environment variable to locate executable file of echo command and then starts the same. The execvp function to be used in a c language program, we need to supply first argument is the name of the program, in this case echo. The shell is searches for its executable using PATH environment variable in this case also. This function execvp requires second argument is an array of character pointers which contains how the command is otherwise executed at the command prompt usually. In this case echo Hello World. Here, echo will be stored as a first string, Hello World as second string. NULL is added to indicate no more arguments to the program

James Bond

Normally, at the shell prompt we can use echo command like this: echo Hello World Here, echo is the command name and Hello World is its argument. When we do so, shell uses PATH environment variable to locate executable file of echo command and then starts the same. The execvp function to be used in a c language program, we need to supply first argument is the name of the program, in this case echo. The shell is searches for its executable using PATH environment variable in this case also. This function execvp requires second argument is an array of character pointers which contains how the command is otherwise executed at the command prompt usually. In this case echo Hello World. Here, echo will be stored as a first string, Hello World as second string. NULL is added to indicate no more arguments to the program

James Bond

I guess people who are used to languages where there is an explicit string type get a little confused when working with C. There's no difference between a pointer to a string and a pointer to a char in C. It's the caller and callee agreeing that the char that is pointed to is actually the first character of a null-terminated sequence of characters that makes the difference. So yes, you're correct, each pointer in the array points to a single char. However, that char is the first character in a sequence of characters, terminated by a null character (zero byte) that forms a C string.

John H

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.