can someone explain to me this code and what the output should look like?, is there going to be two strings? and what does dest[I]=to src[] mean, is string bar going to have the string abcd or efghijk.

#include <stdio.h>

void copy_str(char *dest, const char *src);



int main(void)
{
  char foo[7];
  char bar[7] = {'E', 'F', 'G', 'H', 'I', 'J', 'K'};
  copy_str(bar, "ABCD");
  copy_str(foo, bar);
  return 0;
}


void copy_str(char *dest, const char *src)
{
  int i;
  for (i = 0; src[i] != '\0'; i++)
    dest[i] = src[i];

  // point one 
  dest[i] = '\0';
  return;
}

Recommended Answers

All 3 Replies

As to the output I don't see any output statements (printf and such.) A highly optimizing compiler would strip out lines 9 to 12 as well as the copy_str() function.

commented: just say it did have an output is string bar going to have the string abcd or efghijk. and also what does dest[I]=src[I] mean +0

You could use any of the online c compilers, add one print line and find out. Why make this any harder?

what does dest[i]=src[i] mean

(Note that I corrected your original, incorect line of dest[I] = to src[], which is syntactically wrong and semantically nonsense.)

It copies the current i element of the array pointed to by src to the equivalent location pointed to by dest.

There, that should be a technically correct yet entirely vacuous answer. Any other questions?

More seriously, to really answer the question, we'd have to know what you've been taught about how arrays, pointers, and array indices work in C. Any answer we give as things stand risk being either irritatingly vague, insultingly basic, or excessively advanced.

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.