问题描述:

This might just be the dumbest question, but i havent found any explanation when it comes to char, just when it comes to int, which i can manage.

However, i got this huge problem, i've pointed the adress of my pointer to a char and from that char i want to print out the value.

This is how my code looks:

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

void function(char *pn) {

char text[64];

char nei;

text[64] = &pn;

printf("%s\n", *text[64]);

}

int main()

{

function("Hello");

getchar();

return(0);

}

I can't figure out whats wrong with it to be honest. Why cant I put a * in front of text[64], since thats how you print the value, correct?

网友答案:

'*' has meaning when applied to a pointer. Applied to a char, I'm not sure what it would do. So, no, putting a * in front of text[64] is not how you print the value.

In your code text is a 64 byte long array of char. Valid indexes into it are 0 to 63.

The expression text[64] refers to memory you don't have access to, or may be a reference into one of your other automatic variables. text[64] is beyond the end of the array.

void function(char *pn) {    
    char text[64];
    char nei;
    text[64] = &pn;      /* <- this won't work */
    strcpy(text, pn);    /* <- probably will work, may overflow text if string at pn is too long */

    printf("%s\n", *text[64]);  /* <- this won't work */
    printf("%s\n", pn);         /* <- probably will work */
    printf("%s\n", text);       /* after strcpy above, will likely work */    
}
网友答案:

You cannot use both * and [ ], and also text[64] is out of bounds as its said in comment. You can use function strcpy to copy one string to another.

网友答案:

char text[64]; declares text as an array of 64 characters.

printf("%s\n", *text[64]) makes no sense, because:

  1. text[64] points to the character after the end of the array; i.e. text[63] is the last character in the array.

  2. *text[64] attempts to dereference the 64th item in the text array. But each item is not a pointer, but a character.

Perhaps you meant &text[n] where n is a number between 0 and 63, which would be a pointer to a character within the array, and hence a string. In C a string (what %s expects) is a pointer to a character, and the string is that character and all following up until a NUL.

This bit of your question is unclear:

Why cant I put a * in front of text[64],

(answered above)

since thats how you print the value, correct

I have no idea what that bit means. As per the above, %s expects a char *, i.e. a pointer to a character.

网友答案:

If you are trying to copy the contents of the pointer *pn to the character array text then you better use strcpy() function.

strcpy(text,pn);

will let you copy the contents.

You trying to print out the contents of the array. you can't use the * and [] together. In case of a array for example

char text[64] 

as you have mentioned in your code

printf("%x",text);

will print the start of the allocated memory where the array lies. when you do text[0] the start of the allocated memory address will be added with 0 and the contents of that memory will given back. This is how an array work Now if you are to use a dereferencing operator * then *text will give the 0th element of the array

 *(text + 1) 

will give you the second element of the array and so and so forth. one more thing you can't use

printf("%s",text);

as the value that you are passing to the function is not a string but a character array as it does not have '\0' at its end.

网友答案:

Why cant I put a * in front of text[64]

You can! but not the way you have tried to do it.

For example, in a declaration, char *text[64]; would be used to create an array of strings, each 64 bytes in length. And would require allocation of memory using malloc()orcalloc()`, or an initializer such as:

char *text[64]={"this is a string1","this is string2"};  

Which creates an array of 2 strings, each with 64 bytes (although only partially used), which could be used like this:

printf("%s\n", text[0]);  
printf("%s\n", text[1]);  

since that's how you print the value, correct? (referring to:printf("%s\n", *text[64]);)

No.

The argument char *pn refers to a single string.
If you want to copy this argument to the local variable:
First, change the line:

text[64] = &pn;

to

if(!pn) return; //test and leave if pn is null pointer
strcpy(text, pn);

Then just use:

 printf("%s\n", text);

to print the line

相关阅读:
Top