Any1 plz help me.im new to this site and a begineer of C....
this may be stupid ques to ask..

char msg1[20]="hello";
char msg2[20]="welcome";
char msg3[20]="goodnite";

can i store these three arrays in one sigle array....not in pointers.

ie in.....

char result[50];

i need the result like this..

output: hello welcome goodnite

The simple answer to your question is Yes. What you need to do is creatre a Class eg called info and then call the object of that class msg. then you can pass info to that object and the object with all the info you require can be stored in an array/queue/stack. As i am only new to coding myself this is how I would tackle it as I am in the middle of a similar project at the moment. I have started on pointers and my friends have told me this is a much better method of storing info. I suggest you do a quick search of the tutuorial section of this site or do a google on it as that is where i have learnt about the structs, classes and objects. try www.cplus.com

Regards

Tim

Any1 plz help me.im new to this site and a begineer of C....
this may be stupid ques to ask..

char msg1[20]="hello";
char msg2[20]="welcome";
char msg3[20]="goodnite";

can i store these three arrays in one sigle array....not in pointers.

ie in.....

char result[50];

i need the result like this..

output: hello welcome goodnite

i cant clearly get to ur point...can u expln that n detail and
i cant open that site u specified...

thanx for ur reply.

prasad

>> i cant clearly get to ur point
That's probably because the answer was assuming C++ when you clearly stated C. Arrays are just a bunch of blocks of memory strung together; you can fake a multidimensional array using a single dimensional array easily:

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

int main(void)
{
  char a[60] = "hello";

  strcat(a, " welcome");
  strcat(a, " goodnite");

  puts(a);

  return 0;
}

Or, instead of doing it all manually, you could write a function do to it for you:

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

/* addstr assumes dst points to enough memory
   and the vararg list ends with NULL */
char *addstr(char *dst, ...)
{
  va_list args;
  char *p;

  va_start(args, dst);

  while ((p = va_arg(args, char *)) != NULL)
    strcat(dst, p);

  va_end(args);

  return dst;
}

int main(void)
{
  char a[60] = "";

  addstr(a, "hello", " welcome", " goodnite", NULL);
  puts(a);

  return 0;
}

And of course, if you think you might forget to tag NULL on the end of every call to addstr, you can change it to use an explicit size:

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

char *addstr(char *dst, int n, ...)
{
  va_list args;

  va_start(args, n);

  while (--n >= 0)
    strcat(dst, va_arg(args, char *));

  va_end(args);

  return dst;
}

int main(void)
{
  char a[60] = "";

  addstr(a, 3, "hello", " welcome", " goodnite");
  puts(a);

  return 0;
}

And of course, error checking is a good thing to add, especially for a function with such high risk of buffer overflow:

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

char *addstr(char *dst, int sz, int n, ...)
{
  va_list args;
  int total = 0;

  va_start(args, n);

  while (--n >= 0) {
    char *p = va_arg(args, char *);

    total += strlen(p);

    if (total < sz)
      strcat(dst, p);
  }

  va_end(args);

  return dst;
}

int main(void)
{
  char a[20] = "";

  addstr(a, sizeof a, 3, "hello", " welcome", " goodnite");
  puts(a);

  return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.