Thread: help with char array.

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    9

    help with char array.

    I have a string of numbers that I want to work on as a string.

    Code:
    #include <stdio.h>
    #include <string.h>
    int main()
    {
    	
    	char  num[]= "9781552859544";
    	int i;
    	for (; i<12; i++)
    	{
    		
    		
    		printf("%s", num[i]);
    		
    		
    	}
    	return 0;
    }
    I basically want it to print out each number in turn until it reaches 12 characters, so that I can implement the actual next step.

    However, it's telling me that this array of chars has a type of int and %s expects type char * why is this?

  2. #2
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    The problem is that your value of i is not initialized and thatz why it is takin garbage value

    initialize i with 0

    Code:
    	char  num[]= "9781552859544";
    	int i; // i = 0;

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    9
    I did initialize i. Just to be safe, I even set it to zero just now after your post, still got the error.

    Code:
    #include <stdio.h>
    #include <string.h>
    int main()
    {
    	
    	char  num[]= "9781552859544";
    	int i=0;
    	for (; i<12; i++)
    	{
    		
    		
    		printf("%s", num[i]);
    		
    		
    	}
    	return 0;
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Why exactly are you trying to print a character as a string? Perhaps you would like to use %c instead of %s.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Quote Originally Posted by sup_stephen View Post
    I did initialize i. Just to be safe, I even set it to zero just now after your post, still got the error.

    Code:
    #include <stdio.h>
    #include <string.h>
    int main()
    {
    	
    	char  num[]= "9781552859544";
    	int i=0;
    	for (; i<12; i++)
    	{
    		
    		
    		printf("%s", num[i]);
    		
    		
    	}
    	return 0;
    }
    your modified code

    Code:
    #include <stdio.h>
    #include <string.h>
    int main()
    {
    	
    	char  num[]= "9781552859544";
    	int i=0;
    	for (; i<12; i++)
    	{
    		
    		
    		printf("%c", num[i]);
    		
    		
    	}
    	return 0;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The UNIX System Interface
    By rrc55 in forum C Programming
    Replies: 1
    Last Post: 10-20-2009, 05:56 PM
  2. Replies: 3
    Last Post: 11-17-2008, 12:36 PM
  3. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 09:22 AM
  4. code help required
    By Yobbo in forum C Programming
    Replies: 9
    Last Post: 09-02-2005, 11:15 PM
  5. Creating 2D arrays on heap
    By sundeeptuteja in forum C++ Programming
    Replies: 6
    Last Post: 08-16-2002, 11:44 AM