declaring alpha numeric

Discussion in 'C Programming' started by shan, Nov 16, 2005.

  1. shan

    shan Guest

    how to declare a variable that should contain both alpahabets and
    numbers.It should not be an string or char array.
    shan, Nov 16, 2005
    1. Advertisements

  2. shan

    Skarmander Guest

    int i;
    i = 'a';
    i = '0';

    Skarmander, Nov 16, 2005
    1. Advertisements

  3. Your question is not very clear. Do you mean like this?

    int foo = 'a';

    foo = 123;

    The integer variable foo can be used to hold a letter or a number, is
    what you are after?

    David Resnick, Nov 16, 2005
  4. shan

    pete Guest

    A structure can hold various types simultaneously.
    pete, Nov 16, 2005
  5. shan

    shan Guest

    My question is the variable should hold items like mn995# .
    shan, Nov 16, 2005
  6. Two things.

    1) That looks much like a char array is what is wanted.
    Why don't you want to use one? Explain the use to which
    you intend to put the variable and why a char array doesn't
    meet your needs, and perhaps we can help more? Is this

    2) When replying, please quote stuff so everyone can see the context.
    To quote another poster:
    "If you want to post a followup via, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson

    David Resnick, Nov 16, 2005
  7. Maybe he wants something like a structure holding two chars, one int
    and one char?
    Michel Rouzic, Nov 16, 2005
  8. We don't understand the question. You're misusing the word
    "alphabets"; what exactly do you mean? There are several kinds of
    numbers (integer and floating-point). Do you want a variable that can
    hold different things simultaneously? Why should it not be a string
    or char array?

    Questions of the form "How can I do X without using language feature Y?"
    are often homework (in real life, you're free to use whatever language
    feature will get the job done). Is this a homework assignment?
    Keith Thompson, Nov 16, 2005
  9. shan

    SM Ryan Guest

    # how to declare a variable that should contain both alpahabets and
    # numbers.It should not be an string or char array.

    C doesn't have anything like a Cobol PICTURE (A).
    SM Ryan, Nov 16, 2005
  10. shan a écrit :
    This is nothing but a string.

    If you want to give a semantic to the subfields, it's up to you. a
    structure can hold it.
    Emmanuel Delahaye, Nov 16, 2005
  11. If you want a variable that can contain either numbers or strings,
    you could use a union, but you will have to keep track of which it

    If you mean (contrary to what you say) that you want a string that
    is restricted to alphanumeric characters, there's no way to do that
    in C; you'll have to check it yourself.

    -- Richard
    Richard Tobin, Nov 16, 2005
  12. shan

    Simon Biber Guest

    Here I give you a program in three files. The first is main.c, which
    demonstrates the use of the alphanumeric type. The second is
    alphanumeric.h, which is a header file that you must include to have
    access to the alphanumeric type and the functions that use it. The third
    file contains the implementation of the functions.

    /* main.c */

    #include "alphanumeric.h"

    #include <stdio.h>

    int main(void)
    alphanumeric *p = new_alphanumeric("mn955");
    printf("No, that was not alphanumeric!\n");
    return 0;
    return 0;

    /* alphanumeric.h */

    #ifndef H_ALPHANUMERIC
    #define H_ALPHANUMERIC

    typedef struct alphanumeric alphanumeric;

    alphanumeric *new_alphanumeric(const char *);
    void print_alphanumeric(const alphanumeric *);
    void delete_alphanumeric(alphanumeric *);


    /* alphanumeric.c */

    #include "alphanumeric.h"

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    struct alphanumeric
    char *data;

    static const char *alnum = "0123456789"

    alphanumeric *new_alphanumeric(const char *s)
    size_t n = strlen(s);
    alphanumeric *new;
    if(strspn(s, alnum) != n)
    return NULL;
    new = malloc(sizeof *new);
    return NULL;
    new->data = malloc(n + 1);
    return NULL;
    memcpy(new->data, s, n + 1);
    return new;

    void print_alphanumeric(const alphanumeric *a)
    printf("%s", a->data);

    void delete_alphanumeric(alphanumeric *a)

    /* end of alphanumeric.c */
    Simon Biber, Nov 17, 2005
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.