Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 347 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Iterate through char array and print chars

#1
I am trying to print each char in a variable.

I can print the ANSI char number by changing to this `printf("Value: %d\n", d[i]);` but am failing to actually print the string character itself.

What I am doing wrong here?

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

int main(int argc, char *argv[])
{
int len = strlen(argv[1]);
char *d = malloc (strlen(argv[1])+1);
strcpy(d,argv[1]);

int i;
for(i=0;i<len;i++){
printf("Value: %s\n", (char)d[i]);
}
return 0;
}

Reply

#2
You should use `%c` format to print characters in C. You are using `%s`, which requires to use pointer to the string, but in your case you are providing integer instead of pointer.
Reply

#3
The below will work. You pass in the pointer to a string when using the token %s in printf.

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

int main(int argc, char *argv[])
{
int len = strlen(argv[1]);
char *d = malloc (strlen(argv[1])+1);
strcpy(d,argv[1]);

printf("Value: %s\n", d);
return 0;
}
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through