These string arrays are a pain!

This is a discussion on These string arrays are a pain! within the C Programming forums, part of the General Programming Boards category; Hi, can someone tell me when I have code like this: Code: #import <stdio.h> #import <string.h> int main (int argc, ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    10

    These string arrays are a pain!

    Hi,

    can someone tell me when I have code like this:

    Code:
    #import <stdio.h>
    #import <string.h>
    
    int main (int argc, char const *argv[])
    {
    	int userInput, printableInt, x = 0; 
    
    	char singleDigits[20];
    
    	printf("Enter a positive integer or a negative one to quit: ");
    	scanf("%i", &userInput);
    
    	printf("\n\n");
    
    	while ( userInput > 0 )
    		{
    		printableInt = userInput % 10;
    
    		userInput /= 10;
    
    		switch (printableInt)
    			{
    			case 0:
    				singleDigits[x] = "Zero, ";
    				x += 1;
    				break;
    
    			case 1:
    				singleDigits[x] = "One, ";
    				x += 1;
    				break;
    
    			case 2: 
    				singleDigits[x] = "Two, ";
    				x += 1;
    				break;
    	
    			case 3:
    				singleDigits[x] = "Three, ";
    				x += 1;
    				break;
    
    			case 4:
    				singleDigits[x] = "Four, ";
    				x += 1;
    				break;
    
    			case 5:
    				singleDigits[x] = "Five, ";
    				x += 1;
    				break;
    
    			case 6:
    				singleDigits[x] = "Six, ";
    				x += 1;
    				break;
    			
    			case 7:
    				singleDigits[x] = "Seven, ";
    				x += 1;
    				break;
    
    			case 8:
    				singleDigits[x] = "Eight, ";
    				x += 1;
    				break;
    
    			case 9:
    				singleDigits[x] = "Nine, ";
    				x += 1;
    
    			default:
    				singleDigits[x] = "Unknown ";
    				x += 1;
    				break;
    			}
    		for ( ; x > 0; --x)
    			printf("%s", singleDigits[x]);
    		}
    }
    Do I get the ubiquitous error:

    main.m:24: warning: assignment makes integer from pointer without a cast

    for every line that tries to assign a string to the array at index x?

    I have trolled the forum to try to understand this error and the nearest I came to was the right side of the = is returning an int and the left side is expecting a pointer?

    I am having on going problems trying to assign simple strings to what has to be char arrays??

    thanks in advance

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,845
    char *singleDigits[20];
    would be the thing you need here.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    That's because you are trying to assign a string of characters to a single element of the char array singleDigits[] ie
    Code:
    singleDigits[x] = "Zero, ";
    what you ought to be doing is
    Code:
    singleDigits[] = "Zero, ";

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by FreeCare View Post
    [Why] do I get the ubiquitous error:

    main.m:24: warning: assignment makes integer from pointer without a cast

    for every line that tries to assign a string to the array at index x?

    I have trolled the forum to try to understand this error and the nearest I came to was the right side of the = is returning an int and the left side is expecting a pointer?

    I am having on going problems trying to assign simple strings to what has to be char arrays??

    thanks in advance
    You get that error because you're trying to assign a string to a character.
    You'd still get an error if you tried to assign a string to a string. In C you have to copy every character individually using strcpy() or strncpy()...
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,020
    Quote Originally Posted by Salem View Post
    char *singleDigits[20];
    would be the thing you need here.
    I would make that
    const char* singleDigits[20];

    Quote Originally Posted by itCbitC View Post
    ...what you ought to be doing is
    Code:
    singleDigits[] = "Zero, ";
    That doesn't work. You have to specify the index.

    Anyhow, this is just something you have to deal with due to C being so low level. If you do not like it, there are other higher level languages that can avoid you this pain.
    C++, Java, C#, VB, etc.

    Oh and x += 1 is the same thing as x++.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Just to intrigue you a bit to look at C++, you could not change your code at all except this:
    Code:
    #include <string> //instead of <string.h>
    using namespace std;
    ...
    string singleDigits[20];
    Now you can assign string literals (like "Zero") to a string as you do.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    Oh yes const char *singleDigits[20] is the way to go as in fact they are an array of pointers to char strings.

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    10
    Thank you all for the replies!

    I programmed in C / C++ many moons ago but need to relearn it now.

    I have for many a year now been programming in VB.NET hence by struggle to get back to grips with the low levelness of C, which I do like......just have to remember it all!!

    Shall have a play with the tips provided, thanks again

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,020
    Well then, is there a specific reason for C? C++ will not do?
    Just asking because C++ might be easier if you are used to a higher-level language such as VB dotNet.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Oct 2008
    Posts
    10
    Sure, I have to learn Objective-C and am working thru the book by Stephen Kochan with the aim to getting into programming the iPhone.

    So am in a crash course on Objective C to get up to speed. The chapters I am currently working thru are familiarisation with C I guess before jumping into the object stuff.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 02:02 AM
  2. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 10:19 AM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 04:58 PM
  4. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  5. string handling
    By lessrain in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 08:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21