1

I wrote a small test program to see how I can pass a pointer to a char array to a function. This is my code:

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

int scopy(char *org ){
    printf("the value of org is %s" , org);
    printf("\n");
}

int main(void)
{
    char original[11];
    strcpy(original , "helloworld");

    scopy(original);

    printf("the value of original now is %s" , original);

    return 0;
}

My knowledge of pointers tells me that org is a pointer that is passed the memory address of original[0] , when I execute this program I observe a few things that has confused me and has caused me to doubt the facts I have learnt about pointers.

when I print org I get the complete word back . but according to the logic if pointers isnt this supposed to print out the memory address;

when I try printing out out[1] , out[2] , codepad tells me that the process has ended abnormally

My basic doubt in this question is what exactly is org doing , I understand it is a char pointer pointing to a memory address (which??) . Any help would be appreciated . I am very new to c programming .

Also when I try a while loop like

while(out[i] !='\0')
printf("%s",out[i]);

it does not terminate at the '\0' . the strcpy documentation states that the '\0' is copied to the char array.

2 Answers 2

3

org is a pointer to a char, which is what you're passing in scopy (a pointer to the first element of an array). When you use the specifier %s in printf you are treating a pointer to a char as a string; if you want to print its value as a pointer you should use %p instead. For example: printf("The memory address in org is %p\n", org);

From what you say I get the feeling you think that %s will convert a number to a string. It won't do that, it expects a pointer to the first character of a string.

Remember that the name of an array is equivalent to &array[0], i.e. when you pass the name of an array in a function you are passing a pointer to the first element. So here, when you call scopy(original) it's the same as writing scopy(&original[0]), and since original[0] is a char and & is a pointer to it, that's why your function takes char *.

Sign up to request clarification or add additional context in comments.

Comments

0

Yes, you're right, when you call scopy(original); , scopy gets passed a pointer to the 1. element in the original array.

scopy(original); is the exact same as scopy(&original[0]);

However, you use %s with printf. %s expects the matching argument to be a char pointer , poiting to a sequence of chars, and the end of that sequence is a 0 byte. That's what C calls a string.

That means you can't do e.g. printf("%s",org[i]); , since org[i] is a char, it's not a char pointer into an array that ends with a 0 byte.

You can print a single char by doing prinf["%c", org[0]) `while(out[i] !='\0')

Or for your while loop:

while(out[i] !='\0')
  printf("%c",out[i]);

And you could also try:

while(out[i] !='\0')
  printf("%s",&out[i]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.