Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to figure out why does my code gives me different results are runtime

for instance on first test it works
second test a totally different result
third test no result at all

below is my code :

char *Amy[] = {"Olivia \n Emma \n Ava\n Mia\n Charloth\n Noah \n Liam \n Ben \n Oliver \n will"};

printf("\n Amy's Profile\n");



printf("Student Names :\n %s",*Amy,"\n");

What I have tried:

I haven't try much at all since I don't quite see my mistake
Posted
Updated 22-Mar-19 23:14pm

You're going to have to be more exact about the code you use when the problem occurs: If I paste your code into an online compiler and try it:
C++
#include <stdio.h>

int main()
    {
    printf("Hello World\n");
    char *Amy[] = {"Olivia \n Emma \n Ava\n Mia\n Charloth\n Noah \n Liam \n Ben \n Oliver \n will"};
    printf("\n Amy's Profile\n");
    printf("Student Names :\n %s",*Amy,"\n");
    return 0;
    }
It does the same thing each time I run it:
Hello World                                                                                                                                                    
                                                                                                                                                               
 Amy's Profile                                                                                                                                                 
Student Names :                                                                                                                                                
 Olivia                                                                                                                                                        
 Emma                                                                                                                                                          
 Ava                                                                                                                                                           
 Mia                                                                                                                                                           
 Charloth                                                                                                                                                      
 Noah                                                                                                                                                          
 Liam                                                                                                                                                          
 Ben                                                                                                                                                           
 Oliver                                                                                                                                                        
 will
Which is what I expect.
I would suggest one improvement to your code though:
C++
printf("Student Names :\n %s", Amy[0], "\n");
Is normally a better way of accessing an array than using a pointer. It won't make any difference to your code output though.
 
Share this answer
 
Comments
CPallini 23-Mar-19 4:56am    
5. And 'Hello world' is for free! :-)
OriginalGriff 23-Mar-19 5:00am    
Damn! Do you think I should have charged him for that original code? :laugh:
Quote:
char *Amy[] = {"Olivia \n Emma \n Ava\n Mia\n Charloth\n Noah \n Liam \n Ben \n Oliver \n will"};
The above line defines an array of pointers (to characters) having just one item.
While not technically a mistake, it is probably not what you intended.

Quote:
printf("Student Names :\n %s",*Amy,"\n");
The above line is incorrect (see the printf documentation[^]): your format string specifies just one argument (%s), but you are providing two (amy[0] and "\n"). A decent compiler would produce a warning.

I would have written it this way:
C
#include <stdio.h>

int main()
{
  const char *Amy = "Olivia \n Emma \n Ava\n Mia\n Charloth\n Noah \n Liam \n Ben \n Oliver \n will";
  printf("\n Amy's Profile\n");
  printf("Student Names :\n %s\n", Amy);
  return 0;
}
 
Share this answer
 
Comments
Klaus-Werner Konrad 28-Mar-19 9:59am    
"A decent compiler would produce a warning"

NO !!! printf is a function with a variable Count of Arguments, so a Compiler can never know how many Arguments are right ...
CPallini 28-Mar-19 16:27pm    
$ gcc foo.c
foo.c: In function ‘main’:
foo.c:10:12: warning: too many arguments for format [-Wformat-extra-args]
printf("Student Names :\n %s",*Amy,"\n");
^~~~~~~~~~~~~~~~~~~~~~

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900