When printf() is called with %s it will more or less do the following
for(i = 0; pointer[i] != '\0'; i++)
{
printf("%c", pointer[i]);
}
What you want do is something like
for(i = 0; pointer[i] != '\0'; i++)
{
printf("\n Element is %c", pointer[i]);
}
When you are referencing specific elements in an array you must use a pointer AND an index i.e. pointer[0] in this case 0 is the index and 'pointer' is the pointer. The above for loops will move through the entire array one index at a time because 'i' is the index and 'i' increases at the end of every rotation of loop and the loop will continue to rotate until it reaches the terminating NULL character in the array.
So you might want to try something along the lines of this.
void printoutarray(char *pointertoarray)
{
for(i = 0; i <= 2; i++)
{
printf("\n Element is: %s \n", pointertoarray[i]);
}
}
Answer from John Vulconshinz on Stack OverflowWhen printf() is called with %s it will more or less do the following
for(i = 0; pointer[i] != '\0'; i++)
{
printf("%c", pointer[i]);
}
What you want do is something like
for(i = 0; pointer[i] != '\0'; i++)
{
printf("\n Element is %c", pointer[i]);
}
When you are referencing specific elements in an array you must use a pointer AND an index i.e. pointer[0] in this case 0 is the index and 'pointer' is the pointer. The above for loops will move through the entire array one index at a time because 'i' is the index and 'i' increases at the end of every rotation of loop and the loop will continue to rotate until it reaches the terminating NULL character in the array.
So you might want to try something along the lines of this.
void printoutarray(char *pointertoarray)
{
for(i = 0; i <= 2; i++)
{
printf("\n Element is: %s \n", pointertoarray[i]);
}
}
Does this:
char *pointertoarray = &array[0];
compile without warnings on your compiler? If so, your compiler is broken. Please read the errors and warnings it prints out.
&array[0] is a char**, not a char*
Hi,
I have a string like this, which I'd be entering in the standard input. (Not as arguments, once the program has started, then.)
The word 'Message' is fixed input here .But 'George' is the name and can be replaced with any name (of any length). Also the the message; 'Good Evening' is also variable. User can input any message to stdin.
What the program should do is that, it should print this is return as output:
The above shown is the output format.
I have tried it like this but it doesnt seem to come out right:
char buff[20]; // This has the stdin
for(int i = 7; i <strlen(buff); ++i){ // i=7 because I'm trying to skip 'Message' fprintf(stderr, "(%s)", &buff[i]);
}
I know that the above code doesn't consider all the factors. (I'm confused as how to separate each factor!)
I don't understand how I can consider till the name to be one print and then from the name to the end of the message as another (removing the colon in between) and printing all this together!
Any help would be appreciated.
C Print out a string array - Stack Overflow
c - Printing the contents of a string array using pointers - Code Review Stack Exchange
Help in printing 2 d string array in c
Calling Julia from C: printing array of strings?
Videos
int a, b;
char answer[20][256];//<- array of char[256]
for(a=0; a<20; a++)//<- 0 origin
{
scanf("%255s", answer[a]);//<- Reading of the string with a maximum 255 characters (One is reserved for the termination character('\0'))
}
for(b=0; b<20; b++)
{
printf("%s\n", answer[b]);
}
1)
char answer[256];
is an array of chars, not strings. With the way, you wrote the rest of the code, you would need an array of arrays such as:
char answer[100][256];
2)
scanf() expects char* for the format string %s. So you would only need:
scanf("%s", answer[a]);
3) main() should return int. So it should be: int main(void) or equivalent.
4) Another problem you have likely got wrong (looking at your loops) is that indexing in C is zero-based, not starts from 1.
Given that the code is really simple, I see mostly coding style issues with it.
Instead of this:
char *names[] = { "John", "Mona", "Lisa", "Frank" };
I would prefer either of these writing styles:
char *names[] = { "John", "Mona", "Lisa", "Frank" };
// or
char *names[] = {
"John",
"Mona",
"Lisa",
"Frank"
};
The pNames variable is pointless. You could just use names.
Instead of the while loop, a for loop would be more natural.
This maybe a matter of taste,
but I don't think the Hungarian notation like *pArr is great.
And in any case you are using this pointer to step over character by character,
so "Arr" is hardly a good name.
I'd for go for pos instead. Or even just p.
You should declare variables in the smallest scope where they are used.
For example *pos would be best declared inside the for loop.
In C99 and above, the loop variable can be declared directly in the for statement.
The last return statement is unnecessary.
The compiler will insert it automatically and make the main method return with 0 (= success).
Putting it together:
int main(int argc, char *argv[])
{
char *names[] = { "John", "Mona", "Lisa", "Frank" };
for (int i = 0; i < 4; ++i) {
char *pos = names[i];
while (*pos != '\0') {
printf("%c\n", *(pos++));
}
printf("\n");
}
}
Actually it would be more interesting to use argc and argv for something:
int main(int argc, char *argv[])
{
for (int i = 1; i < argc; ++i) {
char *pos = argv[i];
while (*pos != '\0') {
printf("%c\n", *(pos++));
}
printf("\n");
}
}
Other points not already mentioned:
Use const where possible
It's better to use const where possible so that it's clear when you are intending to modify things and clear when you're not. This helps the compiler help you find bugs early.
Avoid magic numbers
It's better to avoid having numbers like 4 in the program. If you want to change things later, you may well be left wondering "why 4? what does that mean?" Better would be to assign a constant with a meaningful name.
Use putchar rather than printf for single characters
The printf function is very useful, but putchar is often better suited for single-character output. The reason is that printf has to, at runtime, parse the format string, apply the arguments and then emit the results, while putchar only has to pass a character directly to the output. This particular code isn't exactly performance-critical but it's useful to acquire good coding habits from the beginning.
Use C idioms
It's more idiomatic C to have a loop like this for the characters:
while(*pArr) { /* ... */ }
rather than this:
while(*pArr != '\0') { /* ... */ }
Consider using a "sentinel" value for the end of a list
There are two common ways to iterate through a list in C. One is as you already have it, when you know how many values are in the list. The other way is to use a "sentinel" value -- some unique value that tells the program "this is the end of the list." For a list of names such as this program has, a rational choice for a sentinel value would be NULL.
Omit the return 0 from the end of main
Uniquely for main, if the program gets to the end of the function, it automatically does the equivalent of return 0 at the end, so you can (and should) omit it.
Result
Using all of these suggestions, one possible rewrite might look like this:
#include <stdio.h>
int main()
{
const char *names[] = { "John", "Mona", "Lisa", "Frank", NULL };
for (int i=0; names[i]; ++i) {
const char *ch = names[i];
while(*ch) {
putchar(*ch++);
putchar('\n');
}
putchar('\n');
}
}
The straight forward approach is simply:
#include <ansi_c.h>//collection of all ansi C headers files in my environment.
//replace this with appropriate headers supporting your environment
int main()
#define MAX_DIGIT_LEN 10 //memory allocation for char *b
{
int a[5] ={1,2,3,4,5};
int i ;
char *b; //in your original post, you never set
//this pointer to point to any space
//it was therefore an uninitialized
//pointer, with no memory space.
//you must initialize pointers, and allocate some memory.
//Somthing like this will work:
//memory must be MAX_DIGIT_LEN * number of ints in array
b = malloc(MAX_DIGIT_LEN*(sizeof(a)/sizeof(a[0]))+1);//using "sizeof(a)/sizeof(a[0]"
//will accommodate array size changes
//+1 allows for NULL byte at end of char array
sprintf(b, "%d%d%d%d%d", a[0],a[1],a[2],a[3],a[4]);
printf("\n %s",b);
free(b);
return 0;
}
Produces this output:
If you want to use a loop, then consider using (creating) an additional variable:
#include <ansi_c.h>//collection of all ansi C headers files in my environment.
//replace this with appropriate headers supporting your environment
#define MAX_DIGIT_LEN 10 //memory allocation for char *b
int main()
{
int a[5] ={1,2,3,4,5};
int i ;
char *b;
char buf[MAX_DIGIT_LEN];
//create this as an intermediary place
//to store discrete elements of a[]
//arbitrarily chose 10, to hold up to
//9 digit integer, such as 123456789.
//if you anticipate larger digits,
//use a bigger index to create buf[]
//memory must be MAX_DIGIT_LEN * number of ints in array
b = calloc(MAX_DIGIT_LEN*(sizeof(a)/sizeof(a[0]))+1, 1);
//note calloc (rather than malloc) is
//preferable here to initialize
//all memory space with 0 before
//attempting strcat() calls.
for(i=0 ; i<sizeof(a)/sizeof(a[0]) ; i++)
{
sprintf(buf, "%d", a[i]);
strcat(b,buf);
}
printf("\n %s",b);
free(b);
return 0;
}
Produces this output:

You need to correct your for loop (moving with the pointer)
for(i = 0 ; i < 5 ; ++i)
{
sprintf(&b[i], "%d", a[i]);
}