Thread: storing a string of binary into an array

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    21

    storing a string of binary into an array

    Hi,
    I am trying to store a string of binary into 2D array but I keep getting "invalid intilizer error".

    My code is:
    Code:
    #include <stdio.h>
    char store;
    
     char ascitobinary();
    
    int main(void){
    
        char string1[][] =store;
    
    }
    
    
    char ascitobinary(){
    
    	char	x ;
    	int 	y;
    	printf("Input a word to transmit: " );
    	scanf("%c",&x);
    	for(y = 0; y < sizeof(char) * 15; y++){
          	 store=(( x & (1 << y) ) ? '1' : '0' );
          	 puts("");
    	}
    
    	return 0;
    }
    Could someone please help I am driving my self crazy.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    My advice would be to slow down and read your textbook or whatever reference you are using from the beginning. Understand basic concepts before moving on. You seem to post the same thing over and over to various programming forums occasionally throwing random syntax in here and there.

    Before attempting to manipulate a 2D array, you should at least know how to make one. So back up at least to there. You can't assign a string, you need to do a string copy (that may sound familiar).
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    21
    I tried doing that but I keep getting errors.The code for that looks like this:

    Code:
    #include <stdio.h>
    char store;
    char ascitobinary();
    char storeInArray(char a[]);
    
    int main(void){
    
    }
    
    
    char ascitobinary(){
    
         char	x ;
         int 	y;
         printf("Input a word to transmit: " );
         scanf("%c",&x);
         for(y = 0; y < sizeof(char) * 15; y++){
               store=(( x & (1 << y) ) ? '1' : '0' );
               puts("");
          }
    
        return 0;
    }
    
    char storeInArray(char a[]){
    
    	char string1[] =store;
    	char string2[15];
    
    	strcpy(string2, string1);
    
    
    }

  4. #4
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    Do as Dave said.because you lack in basics.
    A C programme starts from main().You have'nt called any function in it.Your functions will not execute.
    Am pretty sure your next post will be
    Code:
    #include <stdio.h>
    char store;
    char ascitobinary();
    char storeInArray(char a[]);
    
    int main(void){
    ascitobinary();
    storeInArray(char a[]);
    }
    
    
    char ascitobinary(){
    
         char	x ;
         int 	y;
         printf("Input a word to transmit: " );
         scanf("%c",&x);
         for(y = 0; y < sizeof(char) * 15; y++){
               store=(( x & (1 << y) ) ? '1' : '0' );
               puts("");
          }
    
        return 0;
    }
    
    char storeInArray(char a[]){
    
    	char string1[] =store;
    	char string2[15];
    
    	strcpy(string2, string1);
    
    
    }
    So plz read a text book.
    Long time no C. I need to learn the language again.
    Help a man when he is in trouble and he will remember you when he is in trouble again.
    You learn in life when you lose.
    Complex problems have simple, easy to understand wrong answers.
    "A ship in the harbour is safe, but that's not what ships are built
    for"

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    21
    I know c programme starts from main(). I forgot to call the function when I was posting. I won't post if you are going to assume my posts rather than helping and I am reading a text book. thanks

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    char store;
    store is a single character, it is not an array.
    Code:
    for(y = 0; y < sizeof(char) * 15; y++){
               store=(( x & (1 << y) ) ? '1' : '0' );
               puts("");
          }
    You continually overwrite this character with values, eventually ending up with it being a '1' or a '0'. But you probably wanted this to automagically concatenate a string that you did not declare.
    Code:
    char string1[] =store;
    Here one might guess that you are trying to copy the automagically concatatenated non-string from above into this local not-quite-a-string.
    Code:
    	char string2[15];
    
    	strcpy(string2, string1);
    And here you seem to blindly shove more syntax at the compiler in hopes that it will read your mind and execute the "do what I mean" instuction.

    Nowhere in this code is a 2D array even declared.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  2. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM