memory violation

This is a discussion on memory violation within the C++ Programming forums, part of the General Programming Boards category; Why does this code give memory violation? #include <stdio.h> int main(void) { char string[80]; char substring[80]; char temp[256]; //int pos ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    15

    memory violation

    Why does this code give memory violation?

    #include <stdio.h>

    int main(void)
    {
    char string[80];
    char substring[80];
    char temp[256];
    //int pos = 0;

    temp[0] = '\0';

    int loc, i, j;

    printf("Enter string: ");
    scanf("%s",&string);

    printf("Enter substring: ");
    scanf("%s",&substring);

    printf("Enter position: ");
    scanf("%d",&loc);

    j=0;

    for(i=0; i < loc-1; i++)
    temp[j++] = string[i];

    for(i=0; substring[i] != '\0'; i++)
    temp[j++] = substring[i];

    for(i=loc-1; string[i] != '\0'; i++)
    temp[j++] = string[i];

    temp[j] = '\0';

    printf("%s",temp);

    return 0;
    }

  2. #2
    Registered User
    Join Date
    Aug 2002
    Posts
    55
    Code:
    scanf("%s",&string); 
    scanf("%s",&substring);
    The name given to an array, is a pointer to the first element of that array. That means that you don't need to use the address of operator (&) with an array, for the call to the scanf() function, like you do with standard identifiers.

    So change those lines to:

    Code:
    scanf("%s",string); 
    scanf("%s",substring);
    John.

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    15
    can't enter multi-words...like

    Enter string : this is a

    can only enter 'a' string..

  4. #4
    Registered User
    Join Date
    Aug 2002
    Posts
    55
    That's a limitation of scanf(). You'll need to read up about the other input/output functions.

    http://www.ssec.wisc.edu/~dglo/c_class/stdio.html

    John.

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    cin.getline(text,lenght);

  6. #6
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    In C++:
    Code:
    #include <string>
    #include <iostream>
    
    int main(void)
    {
       string a;
       string b;
       int pos;
    
       cout << "Enter string: ";
       getline(cin, a, '\n');
    
       cout << "Enter substring: ";
       getline(cin, b, '\n');
    
       cout << "Enter position: ";
       cin >> pos;
    
       if(pos < 0) pos = 0;
       if(pos > a.length()) pos = a.length();
    
       a.insert(pos, b);
    
       cout << a << endl;
    
       return 0;
    }

  7. #7
    Registered User
    Join Date
    Nov 2002
    Posts
    36

    Talking

    Code:
    #include <string>
    #include <iostream>
    
    int main(void)
    {
       string a;
       string b;
       int pos;
    
       cout << "Enter string: ";
       getline(cin, a, '\n');
    
       cout << "Enter substring: ";
       getline(cin, b, '\n');
    
       cout << "Enter position: ";
       cin >> pos;
    
       if(pos < 0) pos = 0;
       if(pos > a.length()) pos = a.length();
    
       a.insert(pos, b);
    
       cout << a << endl;
    
       return 0;
    }
    You didn't mean to forget

    Code:
    using namespace std;
    did you??

    that would make a lot of difference for a newbie like me!
    will be there one day!

    I usually use VStudio 6!

  8. #8
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Originally posted by beginner
    You didn't mean to forget

    Code:
    using namespace std;
    did you??

    that would make a lot of difference for a newbie like me!
    Sorry, I forgot.... on my compiler I don't have to tell 'm to use namespace std, it compiles fine without it. So sometimes I foget to add this line to my code. Sorry...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with insert/delete binary search tree
    By Nazgulled in forum C Programming
    Replies: 39
    Last Post: 03-25-2009, 04:24 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Suggestions on this C style code
    By Joelito in forum C Programming
    Replies: 11
    Last Post: 06-07-2007, 03:22 AM
  4. Shared Memory - shmget questions
    By hendler in forum C Programming
    Replies: 1
    Last Post: 11-29-2005, 01:15 AM
  5. Memory allocation and deallocation
    By Micko in forum C++ Programming
    Replies: 3
    Last Post: 08-19-2005, 06:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21