1

I have these two pointers, amen and ptr, where all the values i assign to amen will also be assigned to ptr.

Can ptr and amen be aliases?

void func(const char *ptr) 
{
  struct samp *test;
  DIR *dp;
  char *amen;
  if(ptr[0]=='c'||ptr[0]=='C')
  strcpy(amen,"c_amen.txt");
  else if()
  ......
  else
  ...
}

so if func is called as func("C");, ptr will have the same vale as amen immediately after line 4.

Plus, what is const for? Shouldn't it be to protect ptr from being changed inside func?

John
  • 794
  • 2
  • 18
  • 34

1 Answers1

1

Currently you cannot make any assumptions about the behavior as the following lines are incorrect:

char *amen;
// ...
strcpy(amen,"c_amen.txt");

You are passing an uninitialized pointer to strcpy, so you need to fix that (it expects that the destination pointer is valid and of appropriate size to hold the copied string).

After that is fixed, yes, amen would point to a string with the same content. Of course, it is going to be cleaned up after the function call exits, so not very useful. You need to pass in the size of the string or call strlen on ptr to figure out how large a buffer to allocate.

const in that context means that you cannot change the content that the pointer points to, i.e., the data is read-only. The pointer itself is not const.

Ed S.
  • 122,712
  • 22
  • 185
  • 265