Getting char array value from a method by using pointer.












-1















I have been trying to get the following to work:
My goal is to use pointers in main() to access elements created in a method().



// takes in address of pointer
int method(char** input) {
char *buffer = malloc(sizeof(char)*10);

buffer[0] = 0x12;
buffer[1] = 0x34;
buffer[2] = 0xab;

*input = & buffer;

printf("%xn", *buffer); // this prints 0x12
printf("%xn", &buffer); // this prints address of buffer example: 0x7fffbd98bf78
printf("%xn", *input); // this prints address of buffer

return 0;
}

int main(){

char *ptr;
method(&ptr);

printf(%pn", ptr); // this prints address of buffer

//this does not seem to print out buffer[0]
printf(%xn", *ptr);

}


I want to print each element of buffer values, as created by the method() by using ptr. Any suggestions on how I can go about doing this?



I am not sure if I am misunderstanding something, but I thought ptr points to address of buffer. Thus, dereferencing would give me buffer[0]?



Thank you.










share|improve this question

























  • &buffer is a pointer to the variable, not where it is pointing (which would be plain buffer). The type of &buffer is char **, while the type of *input is char *.

    – Some programmer dude
    Nov 24 '18 at 11:26













  • Also, if you want to print a pointer with printf you first need to cast it to void *, then use the "%p" format.

    – Some programmer dude
    Nov 24 '18 at 11:27
















-1















I have been trying to get the following to work:
My goal is to use pointers in main() to access elements created in a method().



// takes in address of pointer
int method(char** input) {
char *buffer = malloc(sizeof(char)*10);

buffer[0] = 0x12;
buffer[1] = 0x34;
buffer[2] = 0xab;

*input = & buffer;

printf("%xn", *buffer); // this prints 0x12
printf("%xn", &buffer); // this prints address of buffer example: 0x7fffbd98bf78
printf("%xn", *input); // this prints address of buffer

return 0;
}

int main(){

char *ptr;
method(&ptr);

printf(%pn", ptr); // this prints address of buffer

//this does not seem to print out buffer[0]
printf(%xn", *ptr);

}


I want to print each element of buffer values, as created by the method() by using ptr. Any suggestions on how I can go about doing this?



I am not sure if I am misunderstanding something, but I thought ptr points to address of buffer. Thus, dereferencing would give me buffer[0]?



Thank you.










share|improve this question

























  • &buffer is a pointer to the variable, not where it is pointing (which would be plain buffer). The type of &buffer is char **, while the type of *input is char *.

    – Some programmer dude
    Nov 24 '18 at 11:26













  • Also, if you want to print a pointer with printf you first need to cast it to void *, then use the "%p" format.

    – Some programmer dude
    Nov 24 '18 at 11:27














-1












-1








-1








I have been trying to get the following to work:
My goal is to use pointers in main() to access elements created in a method().



// takes in address of pointer
int method(char** input) {
char *buffer = malloc(sizeof(char)*10);

buffer[0] = 0x12;
buffer[1] = 0x34;
buffer[2] = 0xab;

*input = & buffer;

printf("%xn", *buffer); // this prints 0x12
printf("%xn", &buffer); // this prints address of buffer example: 0x7fffbd98bf78
printf("%xn", *input); // this prints address of buffer

return 0;
}

int main(){

char *ptr;
method(&ptr);

printf(%pn", ptr); // this prints address of buffer

//this does not seem to print out buffer[0]
printf(%xn", *ptr);

}


I want to print each element of buffer values, as created by the method() by using ptr. Any suggestions on how I can go about doing this?



I am not sure if I am misunderstanding something, but I thought ptr points to address of buffer. Thus, dereferencing would give me buffer[0]?



Thank you.










share|improve this question
















I have been trying to get the following to work:
My goal is to use pointers in main() to access elements created in a method().



// takes in address of pointer
int method(char** input) {
char *buffer = malloc(sizeof(char)*10);

buffer[0] = 0x12;
buffer[1] = 0x34;
buffer[2] = 0xab;

*input = & buffer;

printf("%xn", *buffer); // this prints 0x12
printf("%xn", &buffer); // this prints address of buffer example: 0x7fffbd98bf78
printf("%xn", *input); // this prints address of buffer

return 0;
}

int main(){

char *ptr;
method(&ptr);

printf(%pn", ptr); // this prints address of buffer

//this does not seem to print out buffer[0]
printf(%xn", *ptr);

}


I want to print each element of buffer values, as created by the method() by using ptr. Any suggestions on how I can go about doing this?



I am not sure if I am misunderstanding something, but I thought ptr points to address of buffer. Thus, dereferencing would give me buffer[0]?



Thank you.







c pointers






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 11:27









πάντα ῥεῖ

73.2k1076142




73.2k1076142










asked Nov 24 '18 at 11:24









meowDestroyermeowDestroyer

1224




1224













  • &buffer is a pointer to the variable, not where it is pointing (which would be plain buffer). The type of &buffer is char **, while the type of *input is char *.

    – Some programmer dude
    Nov 24 '18 at 11:26













  • Also, if you want to print a pointer with printf you first need to cast it to void *, then use the "%p" format.

    – Some programmer dude
    Nov 24 '18 at 11:27



















  • &buffer is a pointer to the variable, not where it is pointing (which would be plain buffer). The type of &buffer is char **, while the type of *input is char *.

    – Some programmer dude
    Nov 24 '18 at 11:26













  • Also, if you want to print a pointer with printf you first need to cast it to void *, then use the "%p" format.

    – Some programmer dude
    Nov 24 '18 at 11:27

















&buffer is a pointer to the variable, not where it is pointing (which would be plain buffer). The type of &buffer is char **, while the type of *input is char *.

– Some programmer dude
Nov 24 '18 at 11:26







&buffer is a pointer to the variable, not where it is pointing (which would be plain buffer). The type of &buffer is char **, while the type of *input is char *.

– Some programmer dude
Nov 24 '18 at 11:26















Also, if you want to print a pointer with printf you first need to cast it to void *, then use the "%p" format.

– Some programmer dude
Nov 24 '18 at 11:27





Also, if you want to print a pointer with printf you first need to cast it to void *, then use the "%p" format.

– Some programmer dude
Nov 24 '18 at 11:27












1 Answer
1






active

oldest

votes


















0














This a fixed & commented version of your code. Ask in the comments if there is smth. you don't understand.



#include <stdio.h>
#include <stdlib.h>

// takes in address of pointer

//Hex: 0xab is larger than the max value of a signed char.
//Most comilers default to signed char if you don't specify unsigned.
//So you need to use unsigned for the values you chose

int method(unsigned char** input) { //<<< changed
unsigned char *buffer = malloc(sizeof(char)*10);

//Check for malloc success <<< added
if(!buffer)
exit(EXIT_FAILURE);

buffer[0] = 0x12;
buffer[1] = 0x34;
buffer[2] = 0xab;
//I recommend not to mix array notation and pointer notation on the same object.
//Alternatively, you could write:

*buffer = 0x12;
*(buffer + 1) = 0x34;
*(buffer + 2) = 0xab;

//buffer already contains the address of your "array".
//You don't want the address of that address
*input = buffer; //<<< changed (removed &)

printf("%xn", *buffer); // this prints 0x12
//Not casting &buffer will likely work (with compiler warnings
//But it is better to conform. Either use (char *) or (void *)
//<<< added the cast for printf()
printf("%pn", (char *)&buffer); // this prints address of buffer example: 0x7fffbd98bf78
printf("%pn", *input); // this prints address of buffer

return 0;
}

int main(){
unsigned char *ptr;
method(&ptr);

printf("%pn", ptr); // this prints address of buffer

//this does not seem to print out buffer[0]
for(int i = 0; i < 3; i++){
//<<< changed to obtain content of buffer via ptr for loop.
unsigned char buf_elem = *(ptr + i);
printf("buffer[%d] in hex: %xt in decimal: %dn", i, buf_elem, buf_elem);
}

// Don't forget to free the memory. //<<< changed
free(ptr);
}





share|improve this answer


























  • It would help more if you add to your answer what you changed, and why.

    – usr2564301
    Nov 24 '18 at 13:47













  • I wrote that in the code's comments.

    – GermanNerd
    Nov 24 '18 at 13:48











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53457637%2fgetting-char-array-value-from-a-method-by-using-pointer%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














This a fixed & commented version of your code. Ask in the comments if there is smth. you don't understand.



#include <stdio.h>
#include <stdlib.h>

// takes in address of pointer

//Hex: 0xab is larger than the max value of a signed char.
//Most comilers default to signed char if you don't specify unsigned.
//So you need to use unsigned for the values you chose

int method(unsigned char** input) { //<<< changed
unsigned char *buffer = malloc(sizeof(char)*10);

//Check for malloc success <<< added
if(!buffer)
exit(EXIT_FAILURE);

buffer[0] = 0x12;
buffer[1] = 0x34;
buffer[2] = 0xab;
//I recommend not to mix array notation and pointer notation on the same object.
//Alternatively, you could write:

*buffer = 0x12;
*(buffer + 1) = 0x34;
*(buffer + 2) = 0xab;

//buffer already contains the address of your "array".
//You don't want the address of that address
*input = buffer; //<<< changed (removed &)

printf("%xn", *buffer); // this prints 0x12
//Not casting &buffer will likely work (with compiler warnings
//But it is better to conform. Either use (char *) or (void *)
//<<< added the cast for printf()
printf("%pn", (char *)&buffer); // this prints address of buffer example: 0x7fffbd98bf78
printf("%pn", *input); // this prints address of buffer

return 0;
}

int main(){
unsigned char *ptr;
method(&ptr);

printf("%pn", ptr); // this prints address of buffer

//this does not seem to print out buffer[0]
for(int i = 0; i < 3; i++){
//<<< changed to obtain content of buffer via ptr for loop.
unsigned char buf_elem = *(ptr + i);
printf("buffer[%d] in hex: %xt in decimal: %dn", i, buf_elem, buf_elem);
}

// Don't forget to free the memory. //<<< changed
free(ptr);
}





share|improve this answer


























  • It would help more if you add to your answer what you changed, and why.

    – usr2564301
    Nov 24 '18 at 13:47













  • I wrote that in the code's comments.

    – GermanNerd
    Nov 24 '18 at 13:48
















0














This a fixed & commented version of your code. Ask in the comments if there is smth. you don't understand.



#include <stdio.h>
#include <stdlib.h>

// takes in address of pointer

//Hex: 0xab is larger than the max value of a signed char.
//Most comilers default to signed char if you don't specify unsigned.
//So you need to use unsigned for the values you chose

int method(unsigned char** input) { //<<< changed
unsigned char *buffer = malloc(sizeof(char)*10);

//Check for malloc success <<< added
if(!buffer)
exit(EXIT_FAILURE);

buffer[0] = 0x12;
buffer[1] = 0x34;
buffer[2] = 0xab;
//I recommend not to mix array notation and pointer notation on the same object.
//Alternatively, you could write:

*buffer = 0x12;
*(buffer + 1) = 0x34;
*(buffer + 2) = 0xab;

//buffer already contains the address of your "array".
//You don't want the address of that address
*input = buffer; //<<< changed (removed &)

printf("%xn", *buffer); // this prints 0x12
//Not casting &buffer will likely work (with compiler warnings
//But it is better to conform. Either use (char *) or (void *)
//<<< added the cast for printf()
printf("%pn", (char *)&buffer); // this prints address of buffer example: 0x7fffbd98bf78
printf("%pn", *input); // this prints address of buffer

return 0;
}

int main(){
unsigned char *ptr;
method(&ptr);

printf("%pn", ptr); // this prints address of buffer

//this does not seem to print out buffer[0]
for(int i = 0; i < 3; i++){
//<<< changed to obtain content of buffer via ptr for loop.
unsigned char buf_elem = *(ptr + i);
printf("buffer[%d] in hex: %xt in decimal: %dn", i, buf_elem, buf_elem);
}

// Don't forget to free the memory. //<<< changed
free(ptr);
}





share|improve this answer


























  • It would help more if you add to your answer what you changed, and why.

    – usr2564301
    Nov 24 '18 at 13:47













  • I wrote that in the code's comments.

    – GermanNerd
    Nov 24 '18 at 13:48














0












0








0







This a fixed & commented version of your code. Ask in the comments if there is smth. you don't understand.



#include <stdio.h>
#include <stdlib.h>

// takes in address of pointer

//Hex: 0xab is larger than the max value of a signed char.
//Most comilers default to signed char if you don't specify unsigned.
//So you need to use unsigned for the values you chose

int method(unsigned char** input) { //<<< changed
unsigned char *buffer = malloc(sizeof(char)*10);

//Check for malloc success <<< added
if(!buffer)
exit(EXIT_FAILURE);

buffer[0] = 0x12;
buffer[1] = 0x34;
buffer[2] = 0xab;
//I recommend not to mix array notation and pointer notation on the same object.
//Alternatively, you could write:

*buffer = 0x12;
*(buffer + 1) = 0x34;
*(buffer + 2) = 0xab;

//buffer already contains the address of your "array".
//You don't want the address of that address
*input = buffer; //<<< changed (removed &)

printf("%xn", *buffer); // this prints 0x12
//Not casting &buffer will likely work (with compiler warnings
//But it is better to conform. Either use (char *) or (void *)
//<<< added the cast for printf()
printf("%pn", (char *)&buffer); // this prints address of buffer example: 0x7fffbd98bf78
printf("%pn", *input); // this prints address of buffer

return 0;
}

int main(){
unsigned char *ptr;
method(&ptr);

printf("%pn", ptr); // this prints address of buffer

//this does not seem to print out buffer[0]
for(int i = 0; i < 3; i++){
//<<< changed to obtain content of buffer via ptr for loop.
unsigned char buf_elem = *(ptr + i);
printf("buffer[%d] in hex: %xt in decimal: %dn", i, buf_elem, buf_elem);
}

// Don't forget to free the memory. //<<< changed
free(ptr);
}





share|improve this answer















This a fixed & commented version of your code. Ask in the comments if there is smth. you don't understand.



#include <stdio.h>
#include <stdlib.h>

// takes in address of pointer

//Hex: 0xab is larger than the max value of a signed char.
//Most comilers default to signed char if you don't specify unsigned.
//So you need to use unsigned for the values you chose

int method(unsigned char** input) { //<<< changed
unsigned char *buffer = malloc(sizeof(char)*10);

//Check for malloc success <<< added
if(!buffer)
exit(EXIT_FAILURE);

buffer[0] = 0x12;
buffer[1] = 0x34;
buffer[2] = 0xab;
//I recommend not to mix array notation and pointer notation on the same object.
//Alternatively, you could write:

*buffer = 0x12;
*(buffer + 1) = 0x34;
*(buffer + 2) = 0xab;

//buffer already contains the address of your "array".
//You don't want the address of that address
*input = buffer; //<<< changed (removed &)

printf("%xn", *buffer); // this prints 0x12
//Not casting &buffer will likely work (with compiler warnings
//But it is better to conform. Either use (char *) or (void *)
//<<< added the cast for printf()
printf("%pn", (char *)&buffer); // this prints address of buffer example: 0x7fffbd98bf78
printf("%pn", *input); // this prints address of buffer

return 0;
}

int main(){
unsigned char *ptr;
method(&ptr);

printf("%pn", ptr); // this prints address of buffer

//this does not seem to print out buffer[0]
for(int i = 0; i < 3; i++){
//<<< changed to obtain content of buffer via ptr for loop.
unsigned char buf_elem = *(ptr + i);
printf("buffer[%d] in hex: %xt in decimal: %dn", i, buf_elem, buf_elem);
}

// Don't forget to free the memory. //<<< changed
free(ptr);
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 24 '18 at 14:13

























answered Nov 24 '18 at 13:45









GermanNerdGermanNerd

512111




512111













  • It would help more if you add to your answer what you changed, and why.

    – usr2564301
    Nov 24 '18 at 13:47













  • I wrote that in the code's comments.

    – GermanNerd
    Nov 24 '18 at 13:48



















  • It would help more if you add to your answer what you changed, and why.

    – usr2564301
    Nov 24 '18 at 13:47













  • I wrote that in the code's comments.

    – GermanNerd
    Nov 24 '18 at 13:48

















It would help more if you add to your answer what you changed, and why.

– usr2564301
Nov 24 '18 at 13:47







It would help more if you add to your answer what you changed, and why.

– usr2564301
Nov 24 '18 at 13:47















I wrote that in the code's comments.

– GermanNerd
Nov 24 '18 at 13:48





I wrote that in the code's comments.

– GermanNerd
Nov 24 '18 at 13:48




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53457637%2fgetting-char-array-value-from-a-method-by-using-pointer%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Wiesbaden

Marschland

Dieringhausen