Hello,

I'm having quite a hard time getting around this access violation error I keep getting. Every time I declare a character string in a structure I can never access it. Here's a small example of my code that keeps giving me a error no matter what I try.

struct _inputCH{
      char  str[15];
      char input[20];
      int   data;
}inputCH[10];

BOOL newInput(struct _inputCH *currentInput){
     CHAR lpBuffer[200];
     printf("Enter something in:");
     gets(lpBuffer);

     //Accesses Violation 0xC00000005(dst = bad ptr)
     memcpy(&currentInput->input, &lpBuffer, strlen(lpBuffer)+1);

     return TRUE;
}


int main(){
...

  newInput(&inputCH[0]);

...

return 0;
}

Thanks,
-A2H

You'll have to post something you actually tried, rather than some cut-down approximation.

$ cat foo.c
#include <stdio.h>
#include <string.h>

struct _inputCH{
  char  str[15];
  char input[20];
  int   data;
}inputCH[10];

typedef enum { FALSE, TRUE } BOOL;
typedef char CHAR;

BOOL newInput(struct _inputCH *currentInput){
  CHAR lpBuffer[200];
  printf("Enter something in:");
  gets(lpBuffer);
  printf("currentInput=%p\n",(void*)currentInput);
  printf("currentInput->input=%p\n",(void*)&currentInput->input);
  memcpy(&currentInput->input, &lpBuffer, strlen(lpBuffer)+1);
  
  return TRUE;
}

int main(){
  newInput(&inputCH[0]);
  return 0;
}
$ gcc -W -Wall -ansi foo.c
/tmp/cckUrPny.o: In function `newInput':
foo.c:(.text+0x33): warning: the `gets' function is dangerous and should not be used.
$ ./a.out 
Enter something in:hello
currentInput=0x804a060
currentInput->input=0x804a06f

Aside from the dumb use of gets(), there is nothing wrong with the addresses.
http://sourceforge.net/apps/mediawiki/cpwiki/index.php?title=Gets

Not sure if this is what your looking for..

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

struct mystr
{
	char ch1[50];
	char ch2[50];
};

void myfunc(struct mystr *str1, struct mystr *str2)
{
	memcpy(&str2->ch1[0], &str1->ch1[0], sizeof(str1->ch1));	
	memcpy(&str2->ch2[0], &str1->ch2[0], sizeof(str1->ch2));		
}

int main(int argc, char**argv)
{
	struct mystr str1 = {"string one", "string two"};
	struct mystr str2;

	fprintf(stdout, "str1 ch1->%s, ch2->%s\n", str1.ch1, str1.ch2);
	fprintf(stdout, "str2 ch1->%s, ch2->%s\n", str2.ch1, str2.ch2);

	myfunc(&str1, &str2);

	fprintf(stdout, "str1 ch1->%s, ch2->%s\n", str1.ch1, str1.ch2);
	fprintf(stdout, "str2 ch1->%s, ch2->%s\n", str2.ch1, str2.ch2);	
	exit(EXIT_SUCCESS);
}
commented: If it IS what the OP is looking for, why are you GIVING it to him? You should know better. -2

why are you even using memcpy() -- what you want is strcpy(), not memcpy(). memcpy() is normally used to copy blopbs of binary data not standard null-terminated strings.

#
memcpy(&currentInput->input, lpBuffer, strlen(lpBuffer)+1);
#

I think that lpBuffer itself is a pointer. To access memory buffer remove & operator

commented: Nice catch! +19
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.