PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, June 25, 2022

[FIXED] how to define a variable char in symbols?

 June 25, 2022     c, compiler-errors, variables     No comments   

Issue

i am working in c (i am really a beginner at coding), i want to write a functions tha allows me to translate the text i write in the prompt in morse. So i am trying by define my variables from the letter of the elphabet to the respective morse code but i always get a warning of multi-character and don't know how to move on.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <malloc.h>
//funzione per scrivere codice morse
int main()
{
    char a='.-';
    printf("%c",a);
    
    return 0;
}

Solution

What @Barmar told you is correct:

char a[] = ".-";`

You also tell us that you were trying to defining the mapping from letters to morse code. This could be using a struct along with a lookup function to lookup each letter:

#include <stdio.h>

struct map {
    char letter;
    char *morse;
};

// return morse string for letter, or NULL if not found
const char *lookup(const struct map *m, const char letter) {
    for(unsigned i = 0; m[i].letter; i++) {
        if(m[i].letter == letter)
            return m[i].morse;
    }
    return NULL;
}

int main() {
    struct map m[] = {
        { 'A', (char *) ".-" },
        { 'B', (char *) "-..." },
        // ...
        { '\0', NULL }
    };
    printf("%c => %s\n", 'A', lookup(m, 'A'));
}

Once you get a little more experience you can replace the lookup() function with lfind() or if the map is sorted by letter bsearch().



Answered By - Allan Wind
Answer Checked By - Marilyn (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing